【问题标题】:How to send data in json format using AndroidAsyncHttp如何使用 AndroidAsyncHttp 以 json 格式发送数据
【发布时间】:2016-10-17 10:49:36
【问题描述】:

我正在尝试通过 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]

请帮帮我?

【问题讨论】:

标签: java android json android-async-http loopj


【解决方案1】:

我也用这个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() {

这对我来说很好..希望这会有所帮助..

【讨论】:

    【解决方案2】:

    服务器端需要完成JSON格式的转换,客户端只需要在http-body中一一进行即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      相关资源
      最近更新 更多