为什么我总是在java和httpClient中使用post方法(google maps api)得到零结果?

为什么我总是在java和httpClient中使用post方法(google maps api)得到零结果?,java,post,google-maps-api-3,Java,Post,Google Maps Api 3,我正在使用以下post方法: “myApiKey”代表我的google api密钥 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()).ex

我正在使用以下post方法:

“myApiKey”代表我的google api密钥

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() 答复如下:

{
   "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();
}
公共静态字符串预处理位置(字符串位置){
String[]locationSplitted=location.split(\\s*,\\s*);
StringBuilder查询=新建StringBuilder();
对于(int j=0;j
对于希望附加其他参数或标题(例如,键和接受语言)的用户:

private静态字符串post(字符串位置){
试一试{
列表参数=新的ArrayList();
添加(新的BasicNameValuePair(“地址”,地点));
添加(新的BasicNameValuePair(“key”,把你的密钥放在这里);
URIBuilder=新的URIBuilder(“https://maps.googleapis.com/maps/api/geocode/json)设置参数(参数);
内容响应=Request.Post(builder.build()).addHeader(“接受语言”,“en”).execute().returnContent();
返回response.toString();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(URISyntaxException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回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();
        }
    }
试试看,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();
        }
    }

我做了一些调查

看起来,不可能在请求正文中使用带有参数的
POST
请求(例如,它是必需的)。地理编码API等待URL中的参数。所以对于空URL的参数,它返回一个空响应

Post
请求有一个非常有用的功能-可以指定请求参数的编码。对于
GET
请求,我们有相反的情况-需要对请求的参数进行编码。只有字母数字字符不需要编码-请参阅

基于此,@MiguelResendiz示例并不总是有效的。考虑相同的,但简化的方法-它不需要测试的API密钥,并且使用<代码>请求。POST <代码>与使用<代码>请求相同。在这种情况下获得< /P> > < /P>
private static void post(String address) throws Exception {
    StringBuilder http = new StringBuilder();
    http.append("https://maps.googleapis.com/maps/api/geocode/json");
    http.append("?");
    http.append("address=" + address);
    Content resp = Request.Post(http.toString()).execute().returnContent();
    System.out.println(resp);
}
它适用于
post(“Sidney”)

但是当我们发布(“红灯区”)时,我们有一个例外,因为地址有空格

java.lang.IllegalArgumentException:索引61处的查询中存在非法字符

我们可以使用ApacheHTTP客户端进行编码。并执行有效的get请求

String url = "https://maps.googleapis.com/maps/api/geocode/json";
String address = "Red Light District";
URIBuilder builder = new URIBuilder(url).setParameter("address", address);

Content response = Request.Get(builder.build()).execute().returnContent();

System.out.println(response);

它运行得很好。

我做了一些调查

看起来,不可能在请求正文中使用带有参数的
POST
请求(例如,它是必需的)。地理编码API等待URL中的参数。所以对于空URL的参数,它返回一个空响应

Post
请求有一个非常有用的功能-可以指定请求参数的编码。对于
GET
请求,我们有相反的情况-需要对请求的参数进行编码。只有字母数字字符不需要编码-请参阅

基于此,@MiguelResendiz示例并不总是有效的。考虑相同的,但简化的方法-它不需要测试的API密钥,并且使用<代码>请求。POST <代码>与使用<代码>请求相同。在这种情况下获得< /P> > < /P>
private static void post(String address) throws Exception {
    StringBuilder http = new StringBuilder();
    http.append("https://maps.googleapis.com/maps/api/geocode/json");
    http.append("?");
    http.append("address=" + address);
    Content resp = Request.Post(http.toString()).execute().returnContent();
    System.out.println(resp);
}
它适用于
post(“Sidney”)

但是当我们发布(“红灯区”)时,我们有一个例外,因为地址有空格

java.lang.IllegalArgumentException:索引61处的查询中存在非法字符

我们可以使用ApacheHTTP客户端进行编码。并执行有效的get请求

String url = "https://maps.googleapis.com/maps/api/geocode/json";
String address = "Red Light District";
URIBuilder builder = new URIBuilder(url).setParameter("address", address);

Content response = Request.Get(builder.build()).execute().returnContent();

System.out.println(response);

它工作得很好。

生成的URL是什么样子的?这对我来说很有用:也许你不应该指定一个假的API密钥。@v.ladynev我已经在我的代码中放入了我真正的API密钥。我刚刚在这里发布的代码中加入了apikey。我认为这是显而易见的。我已经指定它编辑我的问题now@StackUser您的
prepreprelocation()
不正确,因为地名中可能有其他非字母数字字符,特别是在其他语言中。@v.ladynev:那么,您的URIBuilder对不同于字母数字的语言字符进行编码?生成的URL是什么样子的?这对我来说很有用:也许你不应该指定一个假的API密钥。@v.ladynev我已经在我的代码中放入了我真正的API密钥。我刚刚在这里发布的代码中加入了apikey。我认为这是显而易见的。我已经指定它编辑我的问题now@StackUser您的
prepreprelocation()
不正确,因为地名中可能有其他非字母数字字符,特别是在其他语言中。@v.ladynev:so,您的URIBuilder编码的语言字符不同于字母数字?您的方法似乎可以正常工作并正确返回json!如果可以的话,试着回答这个问题:我已经把你的问题投了赞成票,我打算把它列为最好的