使用 android-async-http (loopj) 发布 JSON/XML [英] POSTing JSON/XML using android-async-http (loopj)
本文介绍了使用 android-async-http (loopj) 发布 JSON/XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 android-async-http 并且非常喜欢它.我在发布数据时遇到了问题.我必须按以下格式将数据发布到 API:-
I am using android-async-http and really liking it. I've run into a problem with POSTing data. I have to post data to the API in the following format: -
<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
执行此操作,但失败了.这是其他方法吗?我也可以 POST 等效的 JSON.有什么想法吗?
As per the documentation, I tried doing it using RequestParams
, but it is failing. Is this any other way to do it? I can POST equivalent JSON too. Any ideas?
推荐答案
Loopj POST 示例 - 从他们的 Twitter 示例扩展而来:
Loopj POST examples - extended from their Twitter example:
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);
这篇关于使用 android-async-http (loopj) 发布 JSON/XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文