0

Sometimes I get the IOException when I try to get address through latitude and longitude via the geocoder class of android.

This occurs rarely and force closes my application.

  private void locAdd(double latitude, double longitude) {
    Geocoder geocoder = new Geocoder(ReportActivity.this,
            Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(latitude,
                longitude, 1);
        Log.e("Addresses", "-->" + addresses);
            } catch (IOException e) {
        e.printStackTrace();
    }
}

This is the code I use to get location. Is there any issue with the code? How do I solve it?

Logcat

   java.io.IOException: Unable to parse response from server
   at android.location.Geocoder.getFromLocation(Geocoder.java:136)
   at com.pstpl.crimeverify.CrimeReportActivity.locAdd(CrimeReportActivity.java:554)
   at com.pstpl.crimeverify.CrimeReportActivity.access$10(CrimeReportActivity.java:549)
WISHY
  • 11,067
  • 25
  • 105
  • 197

1 Answers1

-1

The API documentation,

http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation%28double,%20double,%20int%29

says that IOException will occur "if the network is unavailable or any other I/O problem occurs".

I don't think there is any problem with the code, since it works for "sometimes".

BTW, you are passing latitude1 and longitude as method arguments, but you are using latitude and longitude.

Harikrishnan
  • 3,664
  • 7
  • 48
  • 77
  • ohh yes sorry that was the mistake.... I have both GPS and network enabled but still same – WISHY Sep 30 '13 at 09:48
  • Enabling network doesn't means that it is available all the time. – Harikrishnan Sep 30 '13 at 10:07
  • So then what is the solution? – WISHY Sep 30 '13 at 10:19
  • You can try for three times and still it is not getting, inform the user (or take necessary error handling actions, depending upon the application requirements) also look at http://stackoverflow.com/a/11928881/721597 – Harikrishnan Oct 01 '13 at 04:03
  • Also look at http://stackoverflow.com/questions/10641346/java-io-ioexception-unable-to-parse-response-from-server-geocoder for more solutions – Harikrishnan Oct 01 '13 at 04:04