我正在使用android-async-http,真的很喜欢它。我遇到了POSTing数据的问题。我必须以以下格式将数据张贴至API:
<request>
<notes>Test api support</notes>
<hours>3</hours>
<project_id type="integer">3</project_id>
<task_id type="integer">14</task_id>
<spent_at type="date">Tue, 17 Oct 2006</spent_at>
</request>根据文档,我尝试使用RequestParams完成此操作,但失败了。这是不是还有其他的方法呢?我也可以发布等价的JSON。有什么想法吗?
发布于 2012-12-16 21:19:40
Loopj POST示例-从他们的Twitter示例扩展而来:
private static AsyncHttpClient client = new AsyncHttpClient();通过RequestParams正常发布
RequestParams params = new RequestParams();
params.put("notes", "Test api support");
client.post(restApiUrl, params, responseHandler);要发布JSON:
JSONObject jsonParams = new JSONObject();
jsonParams.put("notes", "Test api support");
StringEntity entity = new StringEntity(jsonParams.toString());
client.post(context, restApiUrl, entity, "application/json",
responseHandler);发布于 2014-10-13 03:56:11
@Timothy的回答对我不起作用。
我定义了StringEntity的Content-Type以使其工作:
JSONObject jsonParams = new JSONObject();
jsonParams.put("notes", "Test api support");
StringEntity entity = new StringEntity(jsonParams.toString());
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
client.post(context, restApiUrl, entity, "application/json", responseHandler);祝你好运:)
发布于 2016-05-02 16:36:50
发布json的更好方法
RequestParams params = new RequestParams();
params.put("id", propertyID);
params.put("lt", newPoint.latitude);
params.put("lg", newPoint.longitude);
params.setUseJsonStreamer(true);
ScaanRestClient restClient = new ScaanRestClient(getApplicationContext());
restClient.post("/api-builtin/properties/v1.0/edit/location/", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});https://stackoverflow.com/questions/13052036
复制相似问题