我试图通过AsyncHttpClient的post方法以json格式将数据发送到服务器,服务器接受这种格式
{
email : 'foo@bar.com'
password:'xxxxxx'
'username:'John Doe'
'lastname:'john'
}
这是我的代码
JSONObject params = new JSONObject();
params.put("last_name","Karimi");
params.put("username","zahid");
params.put("email","zahid.omerzad@gmail.com");
params.put("password","1234566");
StringEntity entity = new StringEntity(params.toString());
Log.d("entity3",entity+"");
String url = "http://192.168.100.12/users";
client.post(context, url, entity, "application/json", handler);
当我记录实体时,结果如下所示
[Content-Type: text/plain; charset=ISO-8859-1,Content-Length: 496,Chunked: false]
请帮帮我?
最佳答案
我也使用这个AsyncHttpClient
我发送json数据的方式是
// postData = your json data
ByteArrayEntity entity = null;
try {
entity = new ByteArrayEntity(postData.toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
asyncHttpClient.post(getActivity(), url, entity, "application/json", new AsyncHttpResponseHandler() {
这对我来说效果很好..希望这会有所帮助..
关于java - 如何使用AndroidAsyncHttp发送json格式的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37850386/