美文网首页
关于OkHttp使用@Body 使用JsonObject的问题

关于OkHttp使用@Body 使用JsonObject的问题

作者: SnowDragonYY | 来源:发表于2018-01-25 12:41 被阅读0次

在通脱OkHttp使用@Body传递参数的时候,使用jsonObject.toString()由于OkHttp的内部处理,传递的Json串,会出现反斜杠如下所示:

          //参数
        JSONObject jsonObject = new JSONObject();
            JSONObject data = new JSONObject();
            data.put("realname", "chyy");
            data.put("mobile", "15122411111");
            data.put("nickname", "SnowDragon");
            data.put("password", Md5Utils.md5("111111"));
            data.put("confirmedPassword", Md5Utils.md5("111111"));
            jsonObject.put("data",data);


goRegister(@Body String data);
"{\"data\":{\"realname\":\"chyy\",\"mobile\":\"15122408041\",\"nickname\":\"SnowDragon\",\"password\":\"96e79218965eb72c92a549dd5a330112\",\"confirmedPassword\":\"96e79218965eb72c92a549dd5a330112\"}}"

如果直接传入JsonObject会是什么情况呢

goRegister(@Body JSONObject data);

{"nameValuePairs":{"data":{"nameValuePairs":{"realname":"chyy","mobile":"15122408041","nickname":"SnowDragon","password":"96e79218965eb72c92a549dd5a330112","confirmedPassword":"96e79218965eb72c92a549dd5a330112"}}}}

嗯?nameValuePairs这个参数是什么鬼?

Retrofit by default uses GSON to convert HTTP bodies to and from JSON. The object which is specified with @Body annotation will be passed to GSON for serialization, which basically converts the JAVA object to JSON representation. This JSON representation will be the HTTP request body.

JSONObject stores all the key-value mapping in a member variable by name nameValuePairs. Here is an excerpt of JSONObject implementation:

public class JSONObject {
    ...
    private final Map<String, Object> nameValuePairs;
    ...
}

When you pass JSONObject to @Body annotation, this JSONObject is seraliazed, hence the HTTP request body contains : {"nameValuePairs": "actual JSON Object"}.
所以才会出现 nameValuePairs 这个参数

如何去掉这个参数呢?

可以新建一个class,这里就叫 HTTPRegisterBody

public class HTTPRegisterBody {
    public BodyData data;

    public static  class BodyData {

        public BodyData(String realname, String mobile, String nickname, String password, String confirmedPassword) {
            this.realname = realname;
            this.mobile = mobile;
            this.nickname = nickname;
            this.password = password;
            this.confirmedPassword = confirmedPassword;
        }

        public String realname;
        public String mobile;
        public String nickname;
        public String password;
        public String confirmedPassword;
    }
}

请求设置

@Headers({"Content-type: application/json", "Accept: */*"})
    @PUT("users")
    Observable<User> goRegister(@Body HTTPRegisterBody data);

   HTTPRegisterBody httpRegisterBody = new HTTPRegisterBody();
   httpRegisterBody.data = new HTTPRegisterBody.BodyData("chyy","15122411111",
                    "SnowDragon",Md5Utils.md5("111111"),Md5Utils.md5("111111"));

//传入即可
RetrofitHelper.getBookAPI().goRegister(httpRegisterBody)

相关文章

网友评论

      本文标题:关于OkHttp使用@Body 使用JsonObject的问题

      本文链接:https://www.haomeiwen.com/subject/pkcvaxtx.html