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 に答える