Android HTTP POST发送json

时间:2013-11-06 03:34:12

标签: android json http-post

{"method": "setActiveApp", "params": [{"uri":"localapp://webappruntime?        url=http://xxx.xxx.xxx.xxx:8125/active","data":""}], "id": 1}

当尝试在HTTP帖子中发送上述json数据时,我总是从服务器获得ILLEGAL JSON响应。实际上wireshark日志显示,而不是上面的数据,下面的json主体(额外的“现在后”params“:”)正在发送。我如何解决这个问题?

{"method": "setActiveApp", "params": "[{"uri":"localapp://webappruntime?        url=http://xxx.xxx.xxx.xxx:8125/active","data":""}]", "id": "1"}

1 个答案:

答案 0 :(得分:1)

使用此代码。添加一些尝试/捕获的味道。

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

final String CODEPAGE = "UTF-8";
HttpPost post = new HttpPost(yourUrl);
post.setEntity(new StringEntity(yourJson.toString(), CODEPAGE));
HttpResponse resp = null;
HttpClient httpclient = new DefaultHttpClient();
resp = httpclient.execute(post);