Search code examples
javaandroidgeocodingreverse-geocodingrequest-timed-out

Reverse Geocoding gives IOException.. why always timed out?


        Geocoder geocode = new Geocoder(MainPage.this);
        List<Address> address = null;
        StringBuilder addressText = null;
        try {
            address = geocode.getFromLocation(latitudeTemp, longitudeTemp,
                    1);
        } catch (IOException e) {
            e.printStackTrace();

        }

        if (address != null && address.size() != 0) {
            Address addr = address.get(0);
            addressText = new StringBuilder();

            if (addr.getCountryName() != null)
                addressText.append(addr.getCountryName()).append(" ");

            if (addr.getAdminArea() != null)
                addressText.append(addr.getAdminArea()).append(" ");
            if (addr.getLocality() != null)
                addressText.append(addr.getLocality()).append(" ");
            if (addr.getSubLocality() != null)
                addressText.append(addr.getSubLocality()).append(" ");
            if (addr.getThoroughfare() != null)
                addressText.append(addr.getThoroughfare()).append(" ");
            if (addr.getFeatureName() != null)
                addressText.append(addr.getFeatureName()).append(" ");

        } else {

            addressText = new StringBuilder()
                    .append("error");
        }
        myAddress2 = addressText;

I am trying to use Reverse Geocoding to get the actual address. And, actually, the code above oftenly works.

however, sometimes it gives IOException, that says "timed out waiting for a response from server" and when I try to use a separate thread to deal with the code, the devices freezes sometimes due to the IOException..

what should I do to avoid the exception?

Plz help...


Solution

  • It sounds like a network or server issue that is out of your control. You could try exponential backoff (example) as a way to deal with it.