使用改造2在表单数据中上传图像不起作用。但是在post man中工作

时间:2018-02-03 05:17:49

标签: android android-studio retrofit2 image-uploading form-data

主要活动

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

接口

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

部分请求正文okhttp已记录

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

这是我从邮递员那里得到的

enter image description here

  

我在StackOverflow流程中尝试了不同的解决方案。但没什么   适合我。这是我第一次发帖提问。所以请原谅我的错误并帮助我改进。

3 个答案:

答案 0 :(得分:0)

试试这个:

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

等方法中传递值
   Call<ResponseBody> addMediaCall = restInterface.addMedia(userId, body);

在休息界面

@Multipart
@POST("media/add")
Call<ResponseBody> addMedia(@Part("user_id") RequestBody userId,
                            @Part MultipartBody.Part post_src);

答案 1 :(得分:0)

更改以下内容 -

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

现在就像现在一样创建Map<String, RequestBody>。对于你必须做的文件 -

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

其中,prepareFilePart是下面的函数 -

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

答案 2 :(得分:0)

首先创建将String转换为RequestBody

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

然后传递字符串参数 - 例如我传递一个字符串参数 user_id

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

然后放图像文件

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

致电api

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

Webservice接口

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