Google Reverse Geocoding网址请求会在Android应用

时间:2017-12-12 09:21:25

标签: android google-maps geocoding reverse-geocoding google-geocoding-api

我试图从Android应用上的坐标获取位置地址。如上所述here,我自己获得了一个API密钥并使用浏览器上的标准URL对其进行了测试,该URL返回了成功的响应。但是当我在我的Android应用上尝试使用相同的网址时,这个 -

  

{

(只是一个开口大括号)是我得到的回应。

这是我的Java代码:

if (bestLocation != null) {
                        locationLatitude = bestLocation.getLatitude();
                        locationLongitude = bestLocation.getLongitude();
                        String latlng = locationLatitude + "," + locationLongitude;
                        final String location_type = "RANGE_INTERPOLATED";
                        final String API_KEY = "MY_WORKING_KEY";
                        final String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="
                                + latlng + "&location_type=" + location_type + "&key=" + API_KEY;
                        new GetLocationAddress(CreateUser.this).execute(url);
                    }

GetLocationAddress

    private static class GetLocationAddress extends AsyncTask<String, Void, String> {
        private WeakReference<CreateUser> reference;

        GetLocationAddress(CreateUser context) {
            reference = new WeakReference<>(context);
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(strings[0]).openConnection();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String result = reader.readLine();
                connection.disconnect();
                Log.i("JSON Response is ", result);                
                return result;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

URL中的所有参数都在浏览器上运行。我通过为调试和发布密钥库设置API指纹,尝试使用SHA-1作为无限制和限制Android应用,但结果是相同的。请帮忙。

更新

我已经检查了API控制台,发现我的所有请求都被视为禁止(错误403),即使API仅限于提供应用程序指纹的Android应用程序。调试和释放指纹的包名称是相同的。我们还发现,在我的应用收到回复后,API请求计数只在控制台中更新了一段时间。

1 个答案:

答案 0 :(得分:0)

像这样使用AsyncTask

private class GetLocationAddress extends AsyncTask<String, Void, String> {
    String server_response;
    private WeakReference<CreateUser> reference;

    GetLocationAddress(CreateUser context) {
        reference = new WeakReference<>(context);
    }

    @Override
    protected String doInBackground(String... strings) {
        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                server_response = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", server_response);
            }

            urlConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Log.e("Response", "" + server_response);
    }
}


// Converting InputStream to String
private String readStream(InputStream in) {
    BufferedReader reader = null;
    StringBuffer response = new StringBuffer();
    try {
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response.toString();
}

致电此AsyncTask班级

new GetLocationAddress(CreateUser.this).execute(url);