Retrofit 2.9.0中的多部分请求为空 [英] Multipart request in Retrofit 2.9.0 is empty
问题描述
我的Multipart Retrofit请求有问题.它总是空!!为什么没有填充?为什么在我用数据填充时发送一个空的Json.
I am having a problem with my Multipart Retrofit request. Its always EMPTY! Why is it not getting filled? Why am I sending an empty Json when I am filling it with data.
我的代码和日志粘贴在下面.谢谢.
My code and log is pasted below. Thanks.
这是我用虚拟数据填充模型的方式
Here is how i am filling the model with dummy data
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的方式
Here is how I am creating the 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接口
And here is my API Interface for the call
@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 请求:(.下面显示的日志仅用于"title"字段.
And finally here is the EMPTY request that I am seeing for all my request fields in the log :(. The log shown below is just for "title" field.
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邮递员的通话正常!
P.S The call is working fine on Postman!
推荐答案
问题出在OkHttpClient
导入!
这篇关于Retrofit 2.9.0中的多部分请求为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!