1

I am working with reverse geocoding in Android. My code successfully worked until yesterday, but now it stopped working on my Android device (samsung S2). But it works in the emulator. When I compile on the device it shows the following errors in logcat:

02-28 12:56:22.800: W/System.err(9048): java.io.IOException: Service not Available
02-28 12:56:22.815: W/System.err(9048):     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
02-28 12:56:22.815: W/System.err(9048):     at in.wptrafficanalyzer.locationreversegeocoding.MainActivity$ReverseGeocodingTask.doInBackground(MainActivity.java:154)
02-28 12:56:22.830: W/System.err(9048):     at in.wptrafficanalyzer.locationreversegeocoding.MainActivity$ReverseGeocodingTask.doInBackground(MainActivity.java:1)
02-28 12:56:22.830: W/System.err(9048):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-28 12:56:22.830: W/System.err(9048):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-28 12:56:22.835: W/System.err(9048):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-28 12:56:22.835: W/System.err(9048):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
02-28 12:56:22.835: W/System.err(9048):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-28 12:56:22.845: W/System.err(9048):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-28 12:56:22.845: W/System.err(9048):     at java.lang.Thread.run(Thread.java:856)
Mad Scientist
  • 18,090
  • 12
  • 83
  • 109
jithu
  • 706
  • 1
  • 9
  • 13

3 Answers3

3

This is a known issue. A possible workaround is that you have to reboot the device. I was told that I works then again.

Bevor
  • 8,396
  • 15
  • 77
  • 141
  • not working.actually it is working successfully till yesterday.is any other settings in my android device for enable this?.in emulator it works fine,actually what is the real issue? – jithu Feb 28 '13 at 07:43
  • WHY does this work? And how do you communicate this to your app users, because surely, I can't tell all users to reboot their devices after the latest update of my app? That's probably bad practice... – marienke Feb 23 '15 at 10:12
  • @marienke I think this problem hardly ever occurs anymore. And yes, I told the users in the app description to reboot, if they run in such problems, because it's not my fault that Google is buggy. But what I can tell is that only 2 of some thousands users had this problems. – Bevor Feb 23 '15 at 17:49
  • Mmm Ok, maybe I experienced something different today, then. My app didn't want to get address information from the latitude and longitude I gave the geocoder, but when I rebooted the device, it was as if something gorgeous reset and I instantly retrieved addresses. I'll investigate tomorrow and post something. – marienke Feb 23 '15 at 18:33
3

It happens caused by multiple reasons. I've had some devices that always did it, while the same code worked fine in others.

A nice workaround is catching the IOException and firing the google maps web api instead;

public static JSONObject getLocationInfo(String address) {

    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="
            + address + "&ka&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return jsonObject;
}

The returned value you can extract like this;

        lon = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location").getDouble("lng");

            lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location").getDouble("lat");
Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
  • I tried this but it showed add "You must use API key". After I added API key,it still shows error "This API project is not authorized to use this API". What should I do ? – Neha May 03 '19 at 04:57
1

it is working now........... use this trick.

simply edit the project.properties

# Project target
target=Google Inc.:Google APIs:16

The reason is that the Geocoder class is present in the core Android framework, but depends on code contributed by the Google APIs to function properly. Even if your AVD includes the Google APIs, your project still needs to be built against that specific build target.

jithu
  • 706
  • 1
  • 9
  • 13