如何使用google api客户端库为java发送带有JSON数据的POST请求?

Har*_*iya 5 android json http-post httprequest google-api-java-client

我正在尝试使用google api客户端库发送帖子请求,但无法成功.

这是我正在使用的片段

UrlEncodedContent urlEncodedContent = new UrlEncodedContent(paramMap);   //paramMap contains email and password keypairs
HttpRequest request = httpRequestFactory.buildPostRequest(new GenericUrl(Constants.PHP_SERVICE_BASE_PATH + mPath) , urlEncodedContent);
String response = request.execute().parseAsString();
Run Code Online (Sandbox Code Playgroud)

我没有得到预期的回应.我认为这是因为邮政参数,即电子邮件和密码没有以正确的格式发送.我需要用JSON发送它们.

注意:我没有将该库用于Google Web服务.

use*_*372 7

我正在使用Map作为JSON的输入.映射是post请求使用的JsonHttpContent的输入.

Map<String, String> json = new HashMap<String, String>();
json.put("lat", Double.toString(location.getLatitude()));
json.put("lng", Double.toString(location.getLongitude()));
final HttpContent content = new JsonHttpContent(new JacksonFactory(), json);
final HttpRequest request = getHttpRequestFactory().buildPostRequest(new GenericUrl(url), content);
Run Code Online (Sandbox Code Playgroud)


Lev*_*rti 1

UrlEncodedContent 用于发布 HTTP 表单内容(Content-Type:application/x-www-form-urlencoded)。如果 Content-Type 是 application/json 你可能应该使用

http://code.google.com/p/google-http-java-client/source/browse/google-http-client/src/main/java/com/google/api/client/http/json/JsonHttpContent。爪哇