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

标签 java post google-maps-api-3

我使用以下发布方法:

“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/

相关文章:

rest - 如果在读取 POST 响应之前网络出现故障怎么办?

javascript - Google Maps API - 获取最接近邮政编码的点

javascript - 重置谷歌地图中的闪烁标记

javascript - 谷歌地图 fitBounds 无法正常工作

Java 平台相关的类继承

java - 使用 GSON 序列化 ArrayList(通用类型)

c# - 发送和接收 post 变量

javascript - 以 Angular 将数据从一个 View 传递到另一个 View

java - 使用 CSS 选择器在关闭 Span 后获取文本

java - 为什么 A->B 不构成 List<A>->List<B>?这不会消除对通配符的需要吗?