我在API中有一个post方法,我想将数据作为JSON原始数据发布到API。从邮递员测试时,它工作正常。从我得到的不同回应中。
Map<String, String> paramObject = new HashMap<>();
paramObject.put("username", "jino");
paramObject.put("password", "12345");
paramObject.put("confirmpassword", "12345");
paramObject.put("email", "jino@gmail.com");
paramObject.put("phone", "1234567898");
UserService service = RetrofitInstance.getRetrofitInstance().create(UserService.class);
Call<ResponseBody> call = service.signUp(paramObject);
服务
@POST("addnewuser.php")
Call<ResponseBody> signUp(@Body Map<String, String> body);
答案 0 :(得分:0)
传递JsonObject而不是Map
:
public interface RetrofitService {
@POST("addnewuser.php")
Call<ResponseBody> signUp(@Body JsonObject body);
}
创建如下:
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("username", "jino");
jsonObject.addProperty("password", "12345");
jsonObject.addProperty("confirmpassword", "12345");
jsonObject.addProperty("email", "jino@gmail.com");
jsonObject.addProperty("phone", "1234567898");
并致电您的Retrofit
服务:
Call<ResponseBody> call = service.signUp(jsonObject);