只能使用带有 WiFi 的 GeoCoder 获取地址,不能使用移动互联网 (LTE)

Can only obtain address using GeoCoder with WiFi, not mobile internet (LTE)

当我 运行 在此处找到 "Location Address" 示例时: https://github.com/googlesamples/android-play-location

应用仅在连接到 WiFi 时获取地址。当我关闭 WiFi 并使用 LTE 时,我看到 "Sorry, the service is not available."

我正在尝试将此代码转移到我自己的项目中,但我希望它能够使用移动互联网和 WiFi 工作。

有人知道为什么会这样吗?

logcat 以下超时:

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)

2014 年的一些帖子表明 Geocoder 可能无法很好地与 LTE 配合使用。参见示例 Geocoder "Timed out waiting for response from server"

建议改为使用网络 API。参见 Google Geocoder service is unavaliable (Coordinates to address)

请报告您取得的任何成功。我也在使用地理编码器,但到目前为止,只是在 Wifi 上(而且它有效)

我不知道如何让地理编码器使用移动互联网工作,所以我求助于使用 google 地图 api,它可以在 wifi 和移动互联网上工作。这是我的实现:

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;
}