gpt4 book ai didi

java - 为什么我在 java 中使用 httpClient 的 post 方法 (google-maps-api) 总是得到 ZERO_RESULTS?

转载 作者:行者123 更新时间:2023-12-01 21:50:13 27 4
gpt4 key购买 nike

我使用以下发布方法:

“myApiKey”代表我的 Google api key

public static void post(){
try {
Content resp = Request.Post("https://maps.googleapis.com/maps/api/geocode/json").bodyForm(Form.form().add("key", "myApiKey").add("address", "Sidney").build()).execute().returnContent();
String response = resp.toString();
System.out.println(response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

在上面的方法中,我使用了 Fluent API( fluent API in the second grey box )响应如下:

{
"results" : [],
"status" : "ZERO_RESULTS"
}

@v.ladyev,我已经通过这样的预处理自行解决了包含空格的地方的问题:

public static String preprocessLocation(String location){
String[] locationSplitted = location.split("\\s*,\\s*");
StringBuilder query = new StringBuilder();
for(int j = 0; j < locationSplitted.length; j++){
if(j != 0){
query.append("+");
}
String[] parts = locationSplitted[j].split(" ");
for(int i = 0; i < parts.length; i++){
query.append(parts[i]);
if((i + 1) < parts.length){
query.append("+");
}
}
if((j + 1) < locationSplitted.length){
query.append(",");
}
}
return query.toString();
}

对于那些想要附加其他参数或 header (例如 key 和接受语言)的人:

private static String post(String place){
try {
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("address",place));
parameters.add(new BasicNameValuePair("key", PUT_HERE_YOUR_KEY));
URIBuilder builder = new URIBuilder("https://maps.googleapis.com/maps/api/geocode/json").setParameters(parameters);
Content response = Request.Post(builder.build()).addHeader("Accept-Language", "en").execute().returnContent();
return response.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

最佳答案

试试这个,bodyForm() 没有添加您的参数,而且我找不到任何类型的文档。

public static void post(){
try {
StringBuilder http = new StringBuilder();
http.append("https://maps.googleapis.com/maps/api/geocode/json");
http.append("?");
http.append("key=myAppKey");
http.append("&");
http.append("address=Sidney");
Request maps = Request.Post(http.toString());
System.out.println(maps.toString());
Response mapsResponse = maps.execute();
Content resp = mapsResponse.returnContent();
String response = resp.toString();
System.out.println(response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

关于java - 为什么我在 java 中使用 httpClient 的 post 方法 (google-maps-api) 总是得到 ZERO_RESULTS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347092/

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