Mus*_*Mus 46 rest post android loopj android-async-http
我正在使用android-async-http而且非常喜欢它.我遇到了POST数据的问题.我必须以下列格式将数据发布到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>
Run Code Online (Sandbox Code Playgroud)
根据文档,我尝试使用它RequestParams,但它失败了.这是其他任何方式吗?我也可以发布等效的JSON.有任何想法吗?
小智 125
Loopj POST示例 - 从他们的Twitter示例扩展:
private static AsyncHttpClient client = new AsyncHttpClient();
Run Code Online (Sandbox Code Playgroud)
通过RequestParams以下方式发布:
RequestParams params = new RequestParams();
params.put("notes", "Test api support");
client.post(restApiUrl, params, responseHandler);
Run Code Online (Sandbox Code Playgroud)
要发布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);
Run Code Online (Sandbox Code Playgroud)
Dan*_*npe 22
@Timothy的回答对我不起作用.
我定义Content-Type的StringEntity,使其工作:
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);
Run Code Online (Sandbox Code Playgroud)
祝好运 :)
发布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) {
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58363 次 |
| 最近记录: |