0

Main Activity

    String user_id = Utility.getPrefs("user_id", Manager_photo.this);
    String user_role_id = Utility.getPrefs("user_role_id", Manager_photo.this);
    RequestBody userId = RequestBody.create(MediaType.parse("text/plain"), user_id);
    RequestBody passRoleId = RequestBody.create(MediaType.parse("text/plain"), user_role_id);
    RequestBody manager_name = RequestBody.create(MediaType.parse("text/plain"), name);
    RequestBody phone_number = RequestBody.create(MediaType.parse("text/plain"), mobile);
    RequestBody address = RequestBody.create(MediaType.parse("text/plain"), m_address);
    RequestBody country = RequestBody.create(MediaType.parse("text/plain"), m_country);
    RequestBody state = RequestBody.create(MediaType.parse("text/plain"), m_state);
    RequestBody city = RequestBody.create(MediaType.parse("text/plain"), m_city);
    RequestBody user_name = RequestBody.create(MediaType.parse("text/plain"), user);
    RequestBody password = RequestBody.create(MediaType.parse("text/plain"), pass);
    RequestBody managerPhoto = RequestBody.create(MediaType.parse("png"), cust_file);


    HashMap body = new HashMap<>();

    body.put("manager_name", manager_name);
    body.put("phone_number", phone_number);
    body.put("address", address);
    body.put("country", country);
    body.put("state", state);
    body.put("city", city);
    body.put("user_name", user_name);
    body.put("password", password);
    body.put("user_id", userId);
    body.put("user_role_id", passRoleId);
    body.put("userfile", managerPhoto);

    Log.i("requserstbody", body.toString());


    Retrofit retrofit = Utility.builder.build();

    MyApiEndpointInterface endpointInterface = retrofit.create(MyApiEndpointInterface.class);

    Call<BasicResponse> call = endpointInterface.addManger(body);

    call.enqueue(new Callback<BasicResponse>() {
        @Override
        public void onResponse(Call<BasicResponse> call, Response<BasicResponse> response) {
         

Interface

 [@Multipart
    @POST("api/rest/manager")
    Call<BasicResponse> addManger(@PartMap Map<String, RequestBody> params);][1]

Part Of request body okhttp logged

 --029eed44-e080-4028-a980-fdb70985e6ef
01-30 10:43:49.184 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Disposition: form-data; name="city"
01-30 10:43:49.184 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Transfer-Encoding: binary
01-30 10:43:49.184 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Type: text/plain; charset=utf-8
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Length: 3
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: fhv
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: --029eed44-e080-4028-a980-fdb70985e6ef
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Disposition: form-data; name="state"
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Transfer-Encoding: binary
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Type: text/plain; charset=utf-8
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Length: 3
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: yih
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: --029eed44-e080-4028-a980-fdb70985e6ef
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Disposition: form-data; name="userfile"
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Transfer-Encoding: binary
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: Content-Length: 28681
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: �PNG
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: 
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: ������IHDR������l��������������i^�������sBIT��O����� ��IDATx�4����Ȏ$�� �̼�=m�00�y�Wk�[y$�>eq��dT�������������͙�����XS����p�U#\�2����>� �   �?~�����,��s��Md�_�<]?����r?M�V:n�Xx��������,6`kM��-�焞�<��yz޳����oh��y}�'8>�>��u�k���s��bɓώ�x�������(�ke����?��M�����Z���S�UX�ϮS{/����9��}�霾k����XZ���^�%��+��%��g��YJf_�Z�U??YH"a�Z��";�\�韟R?����zqپ�k=�R=�q�}��Ժm����Sw�`Z�@ݳ�ݓ���K�����_��������.��ϯ��J>������|�Y���߇���Y��_��>�������'���8�qݕ����s��t]?�%�g���6Ø�uߖXĺt��
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: �������k{pגG¤��[&TW]������6RNpm�b_��!mp�Z\1y!����D:���z��^\�3H�go�������_�MƧ_WMpm�Ư���Y몟�y�g�Ɵ/?�~�=�:���
01-30 10:43:49.185 19296-19691/com.programer.nivin.tourism D/OkHttp: +�z]�������S����z��׽2�V}2��'�w����3�M��{}���c0w��~�(�E�|��t_��޼TX������q0�=�%I�b���:{a���d��E>鶗��?��������������_�d�_��
01-30 10:43:49.186 19296-19691/com.programer.nivin.tourism D/OkHttp: �^�P]y����ԃ���{�=�'�����8?7��q���?3(/�l��H�g��j��_��\�d����Z3������{c��ֽ�U���?Ƥ53w�.�u{C�!P��<�JK���$��4Q��������q��
01-30 10:43:49.186 19296-19691/com.programer.nivin.tourism D/OkHttp: �8��/�3�t�e��y������^-��dn��]�]��=��|2/x����S��F�}�s?w��o�|�z]7�'u�
01-30 10:43:49.186 19296-19691/com.programer.nivin.tourism D/OkHttp: F~�ם!̽>����um��e��_���3������nU�p�US�/i��]Iri6�E��HZY�:Z��j    2X?�sެ��w�♬�J����`���i���t��50P��@��A:�O���rg�hU5We��t���ׯ_�>��������s�G�\Ϫ?^��ye>���3�k�iT�U����&�ٟ�^��0��L��"��u�J׮�n��=�`�=s�U2��[�$���f��)PIi�8s�������3�w��7��R�䵹Q����2�y]�{���Z<5�ڿ6{2��=��|8��p�Sw������s{�*���v���_���\���~V��.���Z�@��)N«^q���*�tWmԵ8��w�jI�]��j��fQ^�WU��u/D(}2�`콓�}���GC�V���Q�T��Vu��T�!���Y��&@�Vx����ģuC��~���S�3�C��<���M�h_�g�u���4p��o�|�����,F{����kY}��  �?Q�إ;V 

Here is what I get when did from postman

enter image description here

I have tried with a different solution in StackOverflow flow. But nothing works for me. And this is the first time I am posting a question. So please forgive the mistakes and help me to improve.

Community
  • 1
  • 1
Savad
  • 1,513
  • 12
  • 17

3 Answers3

1

Try This one:

    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("parameter name", file.getName(), requestFile);
    RequestBody userId= createPartFromString("value");


    private RequestBody createPartFromString(String descriptionString) {
    return RequestBody.create(okhttp3.MultipartBody.FORM, descriptionString);
    }

Pass Value in method like

   Call<ResponseBody> addMediaCall = restInterface.addMedia(userId, body);

inside rest interface

@Multipart
@POST("media/add")
Call<ResponseBody> addMedia(@Part("user_id") RequestBody userId,
                            @Part MultipartBody.Part post_src);
Mahesh Vayak
  • 1,056
  • 12
  • 25
  • This one give error 400 ,Bad request ,And my code works when i hit without image,When i add image ,there is no image getting in server said – Savad Feb 03 '18 at 05:36
  • @savad "parameter name " instead of "userfile" and then if you get an error it's not your code issue. this code work for me. – Mahesh Vayak Feb 03 '18 at 05:40
  • "return RequestBody.create(okhttp3.MultipartBody.FORM, descriptionString)"---why are you creating request body like this.What is the difference – Savad Feb 03 '18 at 06:13
  • "return RequestBody.create(okhttp3.MultipartBody.FORM, descriptionString)" ? you have to convert string to requestbody nothing else – Mahesh Vayak Feb 03 '18 at 06:14
  • that is already doing in my code .."""RequestBody userId = RequestBody.create(MediaType.parse("text/plain"), user_id);"....my question is what is the difference – Savad Feb 03 '18 at 06:17
0

Change things like below -

@Multipart
@POST("api/rest/manager")
Call<BasicResponse> addManger(@PartMap() Map<String, RequestBody> params, @Part MultipartBody.Part file);

Now create your Map<String, RequestBody> as you do now. For file you have to do like -

// create part for file (photo)
MultipartBody.Part body = prepareImagePart("userfile", fileUri);

Where, prepareFilePart is a function below -

@NonNull
private MultipartBody.Part prepareImagePart(String partName, File file) {  
    // create RequestBody instance from file
    RequestBody requestFile =
        RequestBody.create(
            MediaType.parse("image/*"), 
            file
        );

    // MultipartBody.Part is used to send also the actual file name
    return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
0

First of all create method which converts String to RequestBody

 public static RequestBody toRequestBody (String value) {
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
    return body ;
}

then after pass string parameter -for example i pass one string parameter user_id

 Map<String, RequestBody> map = new HashMap<>();
    map.put("user_id", toRequestBody(userId));

then put image file

 File file = new File("file_name");
RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), cust_file);
map.put("userfile", fileBody);

Call api

 Call<BasicResponse> call = endpointInterface.addManger(map);

Webservice interface

@Multipart
    @POST("api/rest/manager")
    Call<BasicResponse> addManger(@PartMap Map<String, RequestBody> params);
Adil
  • 812
  • 1
  • 9
  • 29
  • sorry i could not find any difference between my code and yours ,You created two method for creating request body and multipart file – Savad Feb 03 '18 at 06:11
  • have you done like this ? File file = new File("file_name"); RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), cust_file); map.put("userfile", fileBody); – Adil Feb 03 '18 at 06:17
  • MediaType.parse("image/png") change insted of (MediaType.parse("png") – Adil Feb 03 '18 at 06:20
  • I have tried with different media type including this .No hope – Savad Feb 03 '18 at 06:29
  • 1
    try with this way - http://www.pratikbutani.com/2016/06/android-upload-image-file-using-retrofit-2-0/ – Adil Feb 03 '18 at 06:43
  • 'RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile);' – Adil Feb 03 '18 at 06:43
  • https://stackoverflow.com/questions/34562950/post-multipart-form-data-using-retrofit-2-0-including-image – Adil Feb 03 '18 at 06:45