从Google商家信息中获取数据出错

时间:2015-06-22 12:08:24

标签: 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):

     某人:{

     

06-22 15:42:08.462:D / GetDataFromURL(22138):“error_message”:“此服务需要API密钥。”,

     

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 for serve applicactions”,其他类似“Android应用程序的密钥”并使用它们进行测试?我使用的API密钥是服务器应用程序的API密钥。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:-1)

您需要在Androidmanifest.xml

中的application tag中添加此内容
<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YOUR_API_KEY" />

以下Link可帮助您获取API密钥。