gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:13:10 25 4
gpt4 key购买 nike

我尝试从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com