StringEntity /字符串到HttpEntity

时间:2016-01-04 12:22:08

标签: android json httpentity

我使用loopj库(https://github.com/loopj/android-async-http)并且最近更改为与API 23兼容。

提交' put'请求我会将StringEntity传递给put方法,如下所示:

client.put(CallApplication.getContext(),url, request.StringEntity, responseHandler);

我的StringEntity是一个通过执行以下操作转换为Json的对象:

public static StringEntity getJsonFromObject(Object object) {

    String json = new Gson().toJson(object);
    StringEntity stringEntity = null;
    try {
        stringEntity = new StringEntity(json, HTTP.UTF_8);
        stringEntity.setContentType("text/json");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return stringEntity;
}

现在我需要使用HttpEntity,而且我不清楚如何才能获得它。如何将我的json字符串放入HttpEntity

2 个答案:

答案 0 :(得分:1)

碰巧有一个

cz.msebera.android.httpclient.entity.StringEntity

可供使用。谁知道!

干杯无论如何

答案 1 :(得分:0)

  

您可以添加namevalue对来设置实体

ArrayList nvp = new ArrayList(); nvp.add(new BasicNameValuePair(“keyname”,jsonvalue));

client.setEntity(new UrlEncodedFormEntity(nvp));

相关问题