2012-03-06 5 views
1

Вопрос: Почему Google возвращает ответ INVALID_REQUEST? Я предполагаю, что это связано с тем, что запрос не строится правильно. (Content длина '2')Google Places POST «INVALID_REQUEST» ответ при добавлении места

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" 
} 

Я запустил JSON через http://jsonlint.com/ и она действует. Я также тщательно проверял его на примере JSON API Google Адресов.

ответ

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(); 
Смежные вопросы