问题
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