Google Places API:错误400 :(错误请求)

时间:2012-10-22 04:30:39

标签: java android google-places-api

我正在尝试创建一个使用Google Places API的Android应用,但我希望它能列出目前不支持的一种地方。我知道我可以通过应用程序添加地点,但我遇到了问题。我一直收到以下错误:

错误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 个答案:

答案 0 :(得分: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;
    }
}