2012-10-22 120 views
0

我正嘗試創建一個利用Google Places API的Android應用程序,但我希望它列出當前不受支持的某種類型的地方。我知道我可以通過應用程序添加地點,但我遇到了一個問題。我不斷收到以下錯誤:Google Places API:錯誤400:(錯誤的請求)

錯誤400:(錯誤的請求) 您的客戶發出格式錯誤或非法的請求。

我已經搜索了所有的工作示例提交添加請求的地方api,但我似乎無法找到任何可以告訴我我做錯了什麼。下面是我試過的代碼大部分我各種網站上找到,似乎仍不能得到它的工作之一:

private static final String PLACE_ADD_URL = "https://maps.googleapis.com/maps/api/place/add/json?"; 

public PlacesList addPlace() throws Exception { 

try { 
     Log.v(LOG_KEY,"Adding Place..."); 
     GenericUrl reqUrl = new GenericUrl(PLACE_ADD_URL); 
     reqUrl.put("key", API_KEY); 
     Log.v(LOG_KEY, "Adding Place..."); 

     reqUrl.put("Host: maps.googleapis.com","{\"location\":{\"lat\":39.977112,\"lng\":-74.182799},\"accuracy\":50.0,\"name\":\"Artisan's Brewery & Italian Grill\",\"types\":[\"other\"],\"language\":\"en\":HTTP/1.1}"); 
     Log.v(LOG_KEY, "Requested URL= " + reqUrl); 

     HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT); 
     HttpRequest request = httpRequestFactory.buildGetRequest(reqUrl); 

     Log.v(LOG_KEY, request.execute().parseAsString()); 
     PlacesList place = request.execute().parseAs(PlacesList.class); 

     Log.v(LOG_KEY, "STATUS = " + place.status); 
     Log.v(LOG_KEY, "Place Added is = " + place);  

      return place; 

    } catch (HttpResponseException e) { 
     Log.v(LOG_KEY, e.getMessage()); 
     throw e; 
    } 

    catch (IOException e) { 
     // TODO: handle exception 
     throw e; 
    } 
} 
+1

你是否在android模擬器中運行這個模擬器,並將其升級爲一個像代理商一樣的HTTP代理,並查看你通過線路發送的內容? – Alan

+0

不,我直接從Samsung Galaxy Nexus運行它。我希望有人會有教程或關於如何從應用程序中添加地點的內容。 –

+0

取而代之,嘗試查看HTTP代理中的請求,並查看是否可以手動調整請求以使其工作。 – Alan

回答

0

這裏是結束了,我的工作......

public JSONObject addPlace() throws Exception { 

try { 
     Log.v(LOG_KEY,"Adding Place..."); 
     String nameAdd = "NAME"; // Enter Place Name 
     String typeAdd = "TYPE"; //Enter Place Type 
     double lat = 00.0000; //Enter Place Latitude 
     double lng = -00.00000; //Enter Place Longitude 


     HttpPost post = new HttpPost(PLACE_ADD_URL); 
     String postBody = 
       "{"+ 
         "\"location\": {" + 
         "\"lat\": " + lat + "," + 
         "\"lng\": " + lng + 
         "}," + 
         "\"accuracy\":50.0," + 
         "\"name\": \"" + nameAdd + "\"," + 
         "\"types\": [\"" + typeAdd + "\"]," + 
         "\"language\": \"en\" " + 

        "}"; 
     StringEntity se = new StringEntity(postBody, HTTP.UTF_8); 
     post.setEntity(se); 
     ResponseHandler<String> responseHandler=new BasicResponseHandler(); 
     String responseBody = new DefaultHttpClient().execute(post, responseHandler); 
     JSONObject response = new JSONObject (responseBody); 
     Log.v(LOG_KEY, "Request URL= " + PLACE_ADD_URL); 


     return response; 

    } catch (HttpResponseException e) { 
     Log.v(LOG_KEY, e.getMessage()); 
     throw e; 
    } 

    catch (IOException e) { 
     // TODO: handle exception 
     throw e; 
    } 
}