Google在添加地点时发出POST“INVALID_REQUEST”响应

时间:2012-03-06 09:04:35

标签: java android

问题:为什么Google会返回INVALID_REQUEST响应?我猜它与未正确构造的请求有关。 (内容长度为' 2')

public void addNewPlace(String latitude, String longitude, String name, String type)
{

    int accuracy = 50;
    HttpRequest request;
    try {
        String placeJSON =
            "{"+
              "\"location\": {" +
                "\"lat\": " + latitude + "," +
                "\"lng\": " + longitude +
               "}," + 
              "\"accuracy\":" + accuracy + "," +
               "\"name\": \"" + name + "\"," +
               "\"types\": [\"" + type + "\"]," +
               "\"language\": \"en-EU\" " +
            "}";

        InputStreamContent content = new InputStreamContent("application/json", new ByteArrayInputStream(placeJSON.getBytes("UTF-8")));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType("application/json");

        JsonHttpContent contentJSON = new JsonHttpContent(new JacksonFactory(), content);
        request = t.buildPostRequest(new GenericUrl(PLACES_ADD_URL), contentJSON);
        request.setHeaders(headers);
        request.getUrl().put("key", "....");
        request.getUrl().put("sensor", "true");

        System.out.println("JSON: " + placeJSON);
        System.out.println("URL: " + request.getUrl());
        System.out.println("HEADERS: " + request.getHeaders());
        System.out.println("CONTENT LENGTH:" + request.getContent().getLength());
        System.out.println("POST:" + request.execute().parseAsString());    
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }       
}

以上印刷语句的结果

JSON: {"location": {"lat": 52.4884149,"lng": -1.8925126},"accuracy":50,"name": "test","types": ["restaurant"],"language": "en-EU" }
URL: https://maps.googleapis.com/maps/api/place/add/json?key=........&sensor=true
HEADERS: {Accept-Encoding=gzip, Content-Type=application/json}
CONTENT LENGTH:2
POST:{
    "status" : "INVALID_REQUEST"
}

我已通过http://jsonlint.com/运行JSON并且它有效。我还针对Google Places API示例JSON彻底检查了它。

1 个答案:

答案 0 :(得分:0)

使用ByteArrayContent.fromString(null, placeJSON)并手动构建JSON字符串。

        String placeJSON =
            "{"+
              "\"location\": {" +
                "\"lat\": " + latitude + "," +
                "\"lng\": " + longitude +
               "}," + 
              "\"accuracy\":" + accuracy + "," +
               "\"name\": \"" + name + "\"," +
               "\"types\": [\"" + type + "\"]," +
               "\"language\": \"en\" " +
            "}";

        HttpRequest request;            
        request = t.buildPostRequest(new GenericUrl(PLACES_ADD_URL), ByteArrayContent.fromString(null, placeJSON));

        //Set the Google headers
        GoogleHeaders headers = new GoogleHeaders();

        headers.setContentType("application/json");     
        request.setHeaders(headers);

        request.getUrl().put("key", "....");
        request.getUrl().put("sensor", "true"); 
        request.execute();