Retrofit 2.9.0 中的多部分请求为空
Multipart request in Retrofit 2.9.0 is empty
我的 Multipart Retrofit 请求有问题。它总是 EMPTY! 为什么没有填满?为什么我在用数据填充它时发送一个空的 Json。
我的代码和日志贴在下面。谢谢。
这是我用虚拟数据填充模型的方法
cards = new Cards();
cards.setTitle("TEST_CARD_NAME");
cards.setFirst_name("TEST_NAME");
cards.setLast_name("TEST_NAME");
cards.setDob("11-11-11");
cards.setPhone("03211111111");
cards.setAddress("test_address");
cards.setEmail("test@gmail.com");
cards.setNotes("test_notes");
这是我创建 RequestBody 的方式
RequestBody title = toRequestBody(cards.getTitle(), "text/plain");
RequestBody first_name = toRequestBody(cards.getFirst_name(), "text/plain");
RequestBody last_name = toRequestBody(cards.getLast_name(), "text/plain");
RequestBody dob = toRequestBody(cards.getDob(), "text/plain");
RequestBody phone = toRequestBody(cards.getPhone(), "text/plain");
RequestBody address = toRequestBody(cards.getAddress(), "text/plain");
RequestBody email = toRequestBody(cards.getEmail().toLowerCase(), "text/plain");
RequestBody category_id = toRequestBody(cards.getCategory_id(), "text/plain");
RequestBody card_front = RequestBody.create(MediaType.parse("image/*"), new File(front_pic_path));
RequestBody card_back = RequestBody.create(MediaType.parse("image/*"), new File(back_pic_path));
这是我发送请求的方式
Api.getClient().create(ApiInterface.class).addACard(title, category_id, first_name, last_name, dob, phone, address, email, card_front, card_back).enqueue(new Callback<SingleCardResponseModel>() {
@Override
public void onResponse(Call<SingleCardResponseModel> call, Response<SingleCardResponseModel> response) {
SingleCardResponseModel cardsResponseModel = response.body();
if (cardsResponseModel != null && cardsResponseModel.getStatus()) {
Log.d(TAG, "success " + "addACard " + Utils.formatString(new Gson().toJson(cardsResponseModel)));
on_card_added_success.setVisibility(View.VISIBLE);
TextView card_name = on_card_added_success.findViewById(R.id.card_name);
card_name.setText(cards.getTitle());
iOnLoyaltyCardAdded.onLoyaltyCardAdded(cards);//
} else {
Log.d(TAG, "getStatus error " + "addACard ");
}
MainActivity.stopAnimation();
}
@Override
public void onFailure(Call<SingleCardResponseModel> call, Throwable t) {
Log.d(TAG, "error " + "addACard " + t.getMessage());
MainActivity.stopAnimation();
}
});
这是我的API调用界面
@Multipart
@POST("cards/add")
Call<SingleCardResponseModel> addACard(@Part("title") RequestBody title, @Part("category_id") RequestBody category_id, @Part("first_name") RequestBody first_name, @Part("last_name") RequestBody last_name, @Part("dob") RequestBody dob, @Part("phone") RequestBody phone, @Part("address") RequestBody address, @Part("email") RequestBody email, @Part("card_front") RequestBody card_front, @Part("card_back") RequestBody card_back);
最后是我在日志中看到的所有请求字段的 EMPTY 请求:(。下面显示的日志仅针对“标题”字段。
Content-Type: multipart/form-data; boundary=f9ff54f7-6086-4623-9d68-b59af515aa9f
Content-Length: 1996
--f9ff54f7-6086-4623-9d68-b59af515aa9f
Content-Disposition: form-data; name="title"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 2
{}
P.S 调用在 Postman 上运行良好!
问题出在 OkHttpClient
导入!
我的 Multipart Retrofit 请求有问题。它总是 EMPTY! 为什么没有填满?为什么我在用数据填充它时发送一个空的 Json。
我的代码和日志贴在下面。谢谢。
这是我用虚拟数据填充模型的方法
cards = new Cards();
cards.setTitle("TEST_CARD_NAME");
cards.setFirst_name("TEST_NAME");
cards.setLast_name("TEST_NAME");
cards.setDob("11-11-11");
cards.setPhone("03211111111");
cards.setAddress("test_address");
cards.setEmail("test@gmail.com");
cards.setNotes("test_notes");
这是我创建 RequestBody 的方式
RequestBody title = toRequestBody(cards.getTitle(), "text/plain");
RequestBody first_name = toRequestBody(cards.getFirst_name(), "text/plain");
RequestBody last_name = toRequestBody(cards.getLast_name(), "text/plain");
RequestBody dob = toRequestBody(cards.getDob(), "text/plain");
RequestBody phone = toRequestBody(cards.getPhone(), "text/plain");
RequestBody address = toRequestBody(cards.getAddress(), "text/plain");
RequestBody email = toRequestBody(cards.getEmail().toLowerCase(), "text/plain");
RequestBody category_id = toRequestBody(cards.getCategory_id(), "text/plain");
RequestBody card_front = RequestBody.create(MediaType.parse("image/*"), new File(front_pic_path));
RequestBody card_back = RequestBody.create(MediaType.parse("image/*"), new File(back_pic_path));
这是我发送请求的方式
Api.getClient().create(ApiInterface.class).addACard(title, category_id, first_name, last_name, dob, phone, address, email, card_front, card_back).enqueue(new Callback<SingleCardResponseModel>() {
@Override
public void onResponse(Call<SingleCardResponseModel> call, Response<SingleCardResponseModel> response) {
SingleCardResponseModel cardsResponseModel = response.body();
if (cardsResponseModel != null && cardsResponseModel.getStatus()) {
Log.d(TAG, "success " + "addACard " + Utils.formatString(new Gson().toJson(cardsResponseModel)));
on_card_added_success.setVisibility(View.VISIBLE);
TextView card_name = on_card_added_success.findViewById(R.id.card_name);
card_name.setText(cards.getTitle());
iOnLoyaltyCardAdded.onLoyaltyCardAdded(cards);//
} else {
Log.d(TAG, "getStatus error " + "addACard ");
}
MainActivity.stopAnimation();
}
@Override
public void onFailure(Call<SingleCardResponseModel> call, Throwable t) {
Log.d(TAG, "error " + "addACard " + t.getMessage());
MainActivity.stopAnimation();
}
});
这是我的API调用界面
@Multipart
@POST("cards/add")
Call<SingleCardResponseModel> addACard(@Part("title") RequestBody title, @Part("category_id") RequestBody category_id, @Part("first_name") RequestBody first_name, @Part("last_name") RequestBody last_name, @Part("dob") RequestBody dob, @Part("phone") RequestBody phone, @Part("address") RequestBody address, @Part("email") RequestBody email, @Part("card_front") RequestBody card_front, @Part("card_back") RequestBody card_back);
最后是我在日志中看到的所有请求字段的 EMPTY 请求:(。下面显示的日志仅针对“标题”字段。
Content-Type: multipart/form-data; boundary=f9ff54f7-6086-4623-9d68-b59af515aa9f
Content-Length: 1996
--f9ff54f7-6086-4623-9d68-b59af515aa9f
Content-Disposition: form-data; name="title"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 2
{}
P.S 调用在 Postman 上运行良好!
问题出在 OkHttpClient
导入!