Retrofit2将身体贴为Json [英] Retrofit2 Post body as Json

查看:349
本文介绍了Retrofit2将身体贴为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新Retrofit以使用 Retrofit2 ,我已经设法做了很多事情GET,POST,PUT ......

I was updating Retrofit to use Retrofit2 and I already managed to a lot of things GET, POST, PUT...

但是我有一个请求,我必须发送一个完整的JSON,我设法在Retrofit 1.9中进行,但在Retrofit2中没有支持它。

But i had a request that i have to send a whole JSON I managed to do it in Retrofit 1.9 but in Retrofit2 there is no support for it.

import retrofit.mime.TypedString;

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

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

如何制作 retrofit2

推荐答案

你可以简单地强迫标题为 application / json (正如你所做的那样)并将其作为字符串发送......

You can literally just force the Header to be application/json (as you've done) and send it as a string...

..

Call call = myService.postSomething(
    RequestBody.create(MediaType.parse("application/json"), jsonObject.toString()));
call.enqueue(...)

然后..

interface MyService {
    @GET("/someEndpoint/")
    Call<ResponseBody> postSomething(@Body RequestBody params);
}

或者我在这里遗漏了什么?

Or am I missing something here?

这篇关于Retrofit2将身体贴为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆