android - http post setEntity 使用改造

标签 android http post retrofit

我最近转向 Retrofit,我想用 Retrofit 替换这个 httpost 集实体。我该怎么做。

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("childId", childId);

    HttpPost httpPost = new HttpPost(url);
    StringEntity entity = new StringEntity(jsonObject.toString());
    httpPost.setEntity(entity);

这就是我想做的,但它确实奏效了,

    Observable<Item> getListOfFeed(@Body StringEntity params);

最佳答案

终于找到答案了,添加一个自定义的TypedJsonString类,

public class TypedJsonString extends TypedString {
    public TypedJsonString(String body) {
        super(body);
    }

    @Override public String mimeType() {
        return "application/json";
    }
}

将我的 json 对象转换为 TypedJsonString,

TypedJsonString typedJsonString = new TypedJsonString(jsonObject.toString());

如下更改 api 类,

Observable<Item> getListOfFeed(@Body TypedJsonString typedJsonString);

关于android - http post setEntity 使用改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34939650/

相关文章:

javascript - 不使用 JSONP 对不同域的 AJAX 调用

json - 我在 TIdHttpServer 上的 POST 请求包含奇怪的字符、json 字符串

http - jhipster 错误 400 BAD REQUEST 并保存 $resource

post - axios拦截器在401重试时更改POST请求的内容类型

android - 如何限制特定 iOS 和 Android 应用程序查看/使用的网页内容?

java - 在饼图中隐藏小值 MPAndroidChart

android - 如何在 TextView 中显示已解析的整数字符串

C++ Windows IOCP - HTTP POST 数据丢失

java - SAXParser 无法解析某些字符

http - 通过 401 Unauthorized 提供登录表单