Multipart request in Retrofit 2.9.0 is empty

点点圈 提交于 2021-02-08 08:36:13

问题


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");

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));

Here is how I am sending the request

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();
        }
    });

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);

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 The call is working fine on Postman!


回答1:


The problem was with OkHttpClient import!



来源:https://stackoverflow.com/questions/65734273/multipart-request-in-retrofit-2-9-0-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!