第二种方法,你需要为每一个不同的请求的对象创建一个不同的Model,太麻烦了,这里选择第一种直接构造RequestBody对象:
Retrofit mRetrofit = new Retrofit.Builder()
.baseUrl(HttpConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())//添加gson转换器
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
.client(new OkHttpClient.Builder().build())
.build();
Apis mAPIFunction = mRetrofit.create(Apis.class);
Map<String, Object> params = new LinkedHashMap<>();
params.put(“name”, “吴彦祖”);
params.put(“request”, “123456”);
RequestBody requestBody = RequestBody.create(MediaType.parse(“application/json; charset=utf-8”), JsonHelper.toJSONString(params));
mAPIFunction.login(RequestBody.create(requestBody))
执行后通过抓包查看,请求体如下: