Retrofit2 使用 参数放到body中 以json方式传输
1API 接口设置
@POST("/npwx/wx/xxx/xxxx")
@Headers({“Content-Type: application/json”,“Accept: application/json”})
Call getPhoneVerify(@Body RequestBody phone);
2创建JsonObject对象
RequestBean bean = new RequestBean("参数");
3 联网请求
// 将jsonobject 对象转化成json
Gson gson = new Gson();
String route = gson.toJson(bean);
RequestBody requestBody =
RequestBody.create(MediaType.parse("application/json; Accept: application/json"), route);
Call<QueryCZCity> phoneVerifyCode = HttpManager.getInstance().getHttpClient().
getPhoneVerify(requestBody);
phoneVerifyCode.enqueue(new Callback<QueryCZCity>() {
@Override
public void onResponse(Call<QueryCZCity> call, Response<QueryCZCity> response) {
if (response != null) {
LUtils.d(TAG, "发送成功");
} else {
LUtils.d(TAG, "发送失败");
}
}
@Override
public void onFailure(Call<QueryCZCity> call, Throwable t) {
}
});