Service not available when I try to get address from Geocoder in android?

烈酒焚心 提交于 2019-12-13 03:59:03

问题


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)

回答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.



来源:https://stackoverflow.com/questions/19087601/service-not-available-when-i-try-to-get-address-from-geocoder-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!