0

When I run the "Location Address" sample found here: https://github.com/googlesamples/android-play-location

The app only fetches the address when connected to WiFi. When I turn WiFi off and use LTE I see "Sorry, the service is not available."

I'm trying to transfer this code to my own project but I want this to work using mobile internet as well as WiFi.

Does anyone know why this is happening?

logcat following timeout:

09-28 21:05:40.219  16344-19027/com.google.android.gms.location.sample.locationaddress E/fetch-address-intent-service﹕ Sorry, the service is not available
java.io.IOException: Timed out waiting for response from server
        at android.location.Geocoder.getFromLocation(Geocoder.java:136)
        at com.google.android.gms.location.sample.locationaddress.FetchAddressIntentService.onHandleIntent(FetchAddressIntentService.java:92)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.os.HandlerThread.run(HandlerThread.java:61)
wmcnally
  • 147
  • 2
  • 14

2 Answers2

1

Some posts from 2014 indicate Geocoder may not work as well with LTE. See for example Geocoder "Timed out waiting for response from server"

The suggestion was to use the web API instead. See Google Geocoder service is unavaliable (Coordinates to address)

Please report back with any success you have. I'm using geocoder too but so far, just on Wifi (and it works)

Community
  • 1
  • 1
hg123
  • 111
  • 6
  • I'm going to move onto other things before I try fixing this with the example you provided. I will come back to it and post when I figured it out. Thanks. – wmcnally Sep 30 '15 at 01:43
  • I eventually resorted to using google maps api through http url connection. See my answer below. – wmcnally Nov 01 '15 at 01:58
0

I couldn't figure out how to get geocoder working using mobile internet so I resorted to using google maps api, which works over wifi and mobile internet. Here is my implementation:

try {
            URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" +
                    latitude + "," + longitude + "&sensor=true");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.connect();

            jsonResult = inputStreamToString(conn.getInputStream()).toString();
            JSONObject jsonResponse = new JSONObject(jsonResult);

            Log.d("json result", "Geocoder Status: " + jsonResponse.getString("status"));
            if("OK".equalsIgnoreCase(jsonResponse.getString("status"))) {
                JSONArray addressComponents = ((JSONArray)jsonResponse.get("results")).getJSONObject(0).getJSONArray("address_components");
                for (int i = 0; i < addressComponents.length(); i++) {
                    String addressComponent = ((JSONArray) ((JSONObject) addressComponents.get(i)).get("types")).getString(0);
                    if (addressComponent.equals("locality")) {
                        locality = ((JSONObject) addressComponents.get(i)).getString("long_name");
                    }
                    if (addressComponent.equals("administrative_area_level_1")) {
                        admin1 = ((JSONObject) addressComponents.get(i)).getString("long_name");
                    }
                    if (addressComponent.equals("country")) {
                        countryCode = ((JSONObject) addressComponents.get(i)).getString("short_name");
                    }
                }
                Log.d("json result", locality + "." + admin1 + "." + countryCode);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

private StringBuilder inputStreamToString(InputStream is) {
    String rLine = "";
    StringBuilder answer = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    try {
        while ((rLine = rd.readLine()) != null) {
            answer.append(rLine);
        }
    } catch (IOException e) {
        // e.printStackTrace();
        Toast.makeText(mContext.getApplicationContext(),
                "Error..." + e.toString(), Toast.LENGTH_LONG).show();
    }
    return answer;
}
wmcnally
  • 147
  • 2
  • 14