How to fix getPostalCode-null?

I want to get the zip code of the address located by my phone through address, but null is returned.

The phone has turned on the GPS, the location information can be obtained, but the zip code can not be obtained.

  /**
   * Convert Location to Address
   * @param location Latitude and Longitude
   * @return
   */
  private String getAddress(Location location) {
      // result
      List<Address> result = null;
      String addressLine = "";// address
      try {
          if (location != null) {
              Geocoder gc = new Geocoder(mContext, Locale.getDefault());
              result = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }

      if (result != null && result.get(0) != null) {
          // success
          Address address = result.get(0);
          addressLine = address.getAddressLine(0);


          String postalCode = address.getPostalCode();

          LogUtil.e(Fields.println.Location, "address=======" + addressLine+"===postalCode==="+postalCode);
          LogUtil.e(Fields.println.Location, "result=======" + result.toString());
      }

      return addressLine;
  }

Hope to get help, thank you!

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

In the description of postal code

 /**
 * Returns the postal code of the address, for example "94110",
 * or null if it is unknown.
 */
public String getPostalCode() {
    return mPostalCode;
}

/**
 * Sets the postal code of the address to the given String, which may
 * be null.
 */
public void setPostalCode(String postalCode) {
    mPostalCode = postalCode;
}

so if from last location postal code isn’t fetched successfully then it will be always null.