1

我正在尝试创建一个利用 Google Places API 的 android 应用程序,但我希望它列出当前不支持的地点类型。我知道我可以通过应用程序添加地点,但这样做有问题。我不断收到以下错误:

错误 400:(错误请求)您的客户端发出了格式错误或非法的请求。

我已经搜索了所有关于向places 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;
    }
}
4

1 回答 1

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;
    }
}
于 2013-06-28T01:31:04.663 回答