我想发布以下 JSON,
{
"name": {
"firstName": "f_name",
"lastName": "l_name"
},
"password": "mypassword123",
"email": "test.mail@gmail.com"
}
接口(interface)内部的注册方法是,
@POST("user/createuser")
Call<RegisterResponseModel> register(@Body RegisterModel body);
使用像这样的注册方法,
Name name = new Name("f_name", "l_name");
RegisterModel registerModel = new RegisterModel(name, "mypassword123", "test.mail@gmail.com");
Call<RegisterResponseModel> res = apiService.register(registerModel);
但无法达到我想要的,请帮助我实现我需要的。提前致谢
最佳答案
试试这个
我在尝试仅以 原始 POST 数据
形式时遇到了同样的问题
- 您的 API 接口(interface)
@POST("your_url_here")
Call<Object> getUser(@Body Map<String, String> body);
- 调用您的
类
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
try {
Map<String, String> requestBody = new HashMap<>();
requestBody.put("email", "abc@gmail.com");
requestBody.put("password", "123678");
Call<Object> call=apiInterface.getUser(requestBody);
call.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
try {
JSONObject object=new JSONObject(new Gson().toJson(response.body()));
Log.e("TAG", "onResponse: "+object );
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
关于android - 如何使用 POST 方法在 android 中使用 Retrofit 发送原始 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100284/