java - 从 Google 地方信息获取数据时出错

标签 java android google-places-api

我尝试从 Google Places API 获取地点,但没有获得任何值。我总是遇到同样的错误,但我不知道为什么。

    public JSONObject getJSONFromUrl() {

    URL url;
    HttpsURLConnection urlConnection;
    JSONObject result = new JSONObject();

    // Making HTTP request
    try {

        //Define connection
        url = new URL("https://maps.googleapis.com/maps/api/place/nearbysearch/json");
        urlConnection = (HttpsURLConnection)url.openConnection();

        urlConnection.setRequestMethod("POST");
        //urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("charset", "utf-8");
        urlConnection.setRequestProperty("Accept", "application/json");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);

        Log.d(TAG, "Conexión establecida");

        //Send data
            String parameters = "?location=38.26,-0.70";
            parameters+="&radius=5000";
            parameters+="&key=API_KEY_SERVER_APPLICATIONS";
            parameters+="&types=restaurant";


        OutputStreamWriter wr= new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
        wr.write(parameters);
        wr.flush();
        wr.close();

        Log.d(TAG, "Datos enviados");
        Log.d(TAG, "ResponseCode: " + String.valueOf(urlConnection.getResponseCode()));

        //Display what returns POST request

        StringBuilder sb = new StringBuilder();

        int HttpResult = urlConnection.getResponseCode();

        if(HttpResult == HttpURLConnection.HTTP_OK){
            String json;

            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));

            String line;

            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }

            br.close();

            //System.out.println(""+sb.toString());
            Log.d(TAG, "sb: " + sb.toString());


            json = sb.toString();
            Log.e("JSON", json);

            // Parse the String to a JSON Object
            result = new JSONObject(json);

        }else{
            //System.out.println(urlConnection.getResponseMessage());
            Log.d(TAG, "urlConnection.getResponseMessage(): " + urlConnection.getResponseMessage());
            result = null;
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        Log.d(TAG, "UnsuppoertedEncodingException: " + e.toString());
    } catch (JSONException e) {
        e.printStackTrace();
        Log.d(TAG, "Error JSONException: " + e.toString());
    } catch (IOException e) {
        e.printStackTrace();
        Log.d(TAG, "IOException: " + e.toString());
    }

    // Return JSON Object
    return result;

}

我总是遇到的错误是:

06-22 15:42:08.456: D/GetDataFromURL(22138): ResponseCode: 200

06-22 15:42:08.462: D/GetDataFromURL(22138):

sb: {

06-22 15:42:08.462: D/GetDataFromURL(22138): "error_message" : "This service requires an API key.",

06-22 15:42:08.462: D/GetDataFromURL(22138): "html_attributions" : [],

06-22 15:42:08.462: D/GetDataFromURL(22138): "results" : [],

06-22 15:42:08.462: D/GetDataFromURL(22138): "status" : "REQUEST_DENIED"

}

注意:之前,我创建了一个 API KEY(如“服务应用程序 key ”)和其他 API key (如“Android 应用程序 key ”)并使用两者进行了测试?我使用的 API key 是服务器应用程序的 API key 。

最佳答案

关于java - 从 Google 地方信息获取数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30979635/

相关文章:

Java mouseClicked() Canvas 不更新

vue.js - 如何在 Google Places API 中使用 VuetifyJS 高级插槽示例

java - HTTP 状态 404 -/FirstSpringMVC/welcome.html

java - 获取重载方法的方法句柄

java - Android:标签和搜索栏

google-maps-api-3 - Google Places API - 查找最近的地点、距离和旅行时间

google-places-api - 来自服务器请求的 Google Places REQUEST_DENIED - 没有任何效果

android - 不使用实时绑定(bind)以编程方式构建 TListView

android - 如何在警报输入对话框中要求非空白文本

android - 将 Android Web 服务连接到本地主机