2

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

当提交一个“put”请求时,我会StringEntity像这样传递到 put 方法:

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

MyStringEntity是一个通过执行以下操作转换为 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

4

2 回答 2

1

碰巧有一个

cz.msebera.android.httpclient.entity.StringEntity

可以使用。谁知道!

无论如何干杯

于 2016-01-04T13:18:56.960 回答
0

您可以添加名称值对来设置实体

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

client.setEntity(new UrlEncodedFormEntity(nvp));

于 2016-01-04T12:36:58.107 回答