无法从改造2中发布多部分数据 [英] Cannot POST multipart data from retrofit 2

查看:82
本文介绍了无法从改造2中发布多部分数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须以这种格式发送发帖请求.

I have to send a post request in this format.

--__X_PAW_BOUNDARY__
Content-Disposition: form-data; name="user_photo[image]"; filename="file.jpg"
Content-Type: image/jpeg

ÿØÿàJFIFHHÿáLExifMM*i    
ÿí8Photoshop 3.08BIM8BIM%ÔÙ²é   ìøB~ÿÀ
 "ÿÄ    
ÿĵ}!1AQa"q2¡#B±ÁRÑð$3br    
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ 
ÿĵw!1AQaq"2B¡±Á    #3RðbrÑ
$4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÛC                                       ÿÛC                                                                                                                                                                                                         ÿÝZÿÚ?ü_¢+þæð¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¢(¯ë3þ

我基本上是尝试在发帖请求中发送名称为user_photo[image]的照片,如下图所示:

I am basically trying to send a photo in post request with name user_photo[image] like the one on the image shown below:

以下是我考虑过的解决方案的摘要:

Here are snippets of the solutions I have considered:

    Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part MultipartBody.Part file);


        MultipartBody.Part body = MultipartBody.Part.createFormData("user_photo[image]", file.getName(), requestFile);
Call<models.UploadResponse> call = userRequest.uploadPhoto(62, body);



    RequestBody requestFile =
            RequestBody.create(MediaType.parse("image/*"), FileUtils.getFile(PhotoUploadActivity.this, fileUri));
    Call<models.UploadResponse> call = userRequest.uploadPhoto(62, requestFile);

    Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part ("name=\"user_photo[image]\"") RequestBody file);

    @Multipart
        @POST("users/{id}/user_photos")
    Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part("name=\"user_photo[image]\"") RequestBody file);

这是okhttp日志:

This is the okhttp log:

D/OkHttp: Content-Type: multipart/form-data; boundary=75e8ae2e-a160-413f-82d1-3afd94f22c43
D/OkHttp: Content-Length: 43706
D/OkHttp: ������JFIF�����������������Photoshop 3.0��8BIM����������hg��vdP4uk_QnzZ02cW1f845(��JFBMD0f00078d0300006c1d0000675000009d53000068590000c77e000041a90000b1a90000��ICC_PROFILE������lcms����mntrRGB XYZ ���������)��9acspAPPL�����������������������������������������������������������������-lcms����������������������������������������������������������������������������������������������
D/OkHttp: desc�������������^cprt����\������wtpt����h������bkpt����|������rXYZ�����������gXYZ�����������bXYZ�����������rTRC�����������@gTRC�����������@bTRC�����������@desc��������������c2��������������������������������������������������������������������������������������������������������������������������������������������������������������������text��������FB����XYZ �������������������������-XYZ ����������������3�����XYZ ������������o�����8������XYZ ������������b������������XYZ ������������$������������curv����������������������c�k�?Q4!�)�2;�FQw]�kpz���|�i�}���0������C��
D/OkHttp: 
D/OkHttp:   
D/OkHttp: �"�������������������������������������������������������������������������<r9������J�������@��aCLk@�|2�=�VW�}������E���� �,i�v!�Q;j�H�N@E0!��1�J�T5L4�������� ��h���U��S�׿м�6v���2��]
D/OkHttp: ����3��T0��-@t�`&h��������%̬�Η�����[w(��جƥ�f^
D/OkHttp: C���;���t������E�͂I�O�[�Hvݝ -�g߫t�k���y����ة�ۼ�6S7zEe��/��I_��b��;�Gr��/�:B��R-7$eO��M��G�۱Iū�G2�r�s7#p�P��Ha�o6U�$�3(�����G\�q�4�ȋ�Dj�S�������n�V���J�f?m{{��ޣq�<\��_L���v+�1��-�\h���\u�Pnms����9   ��".~N�z�*J>f�I�:$��T   j�C�{���$�_$3y���ߪy�¼žj���j�愰�s�z�)�o���J����]�˭���ŧ��s]���
D/OkHttp: ��)��(�u�����şLl_T���S��"(��_o�{n�3���}OU�P`}C^zl�]޹V"}��u�Un|=���ο-�mn�e�1�g���S���Q0ދggF������B�;ӫ>��ӷ�P���5c�x"�#o�9螗�s`�j2����O�Y�ӆa��-�便�zy���d���
D/OkHttp: E�r�jqL�ah�^�u��'�+�Z����N:{a�'N��1���H�M�t �H���i���û�Ԕ�hP
D/OkHttp: ���0���
D/OkHttp: �����Y#M�qg���+��?��T�����ܱ���4�@��CA@����������&����G����wL���<Vo�l�Q�۲����H_?a�I�ũT��z��H���Z����S�b)���d���L11�3������hL �����h����b� ����
D/OkHttp: <Rf"��L��(�
D/OkHttp: s��3�Pgı��`g��ω��>&|J�S%���]�b�m�q#Ө�k��

也:

我提到的解决方案:

  1. https://github.com/square/retrofit/issues/1140
  2. https://futurestud.io/blog /retrofit-2-如何将文件上传到服务器
  3. Retrofit-分段请求:必需的MultipartFile参数文件"不存在
  4. Multipart Retrofit 2.0图像上传
  5. https://github.com/square/retrofit/issues/1063
  1. https://github.com/square/retrofit/issues/1140
  2. https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server
  3. Retrofit - Multipart request: Required MultipartFile parameter 'file' is not present
  4. Multipart Retrofit 2.0 image upload
  5. https://github.com/square/retrofit/issues/1063

我厌倦了寻找解决方案.有人可以帮忙吗?

And I am tired of searching for solutions. Can anyone help?

推荐答案

在API端

  @Multipart
  @POST("users/{id}/user_photos")
  Call<SignUpResp> uploadPhoto(@Part("description") RequestBody description, @Part MultipartBody.Part file,(add if more parameter req as like ID,name));

在Java文件端

    if (fileUpload != null) {


        /**
         * code for multipart
         */

        // create RequestBody instance from file
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), fileUpload);

        // MultipartBody.Part is used to send also the actual file name
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("user_photo[image]", fileUpload.getName(), requestFile);

        // add another part within the multipart request
        String descriptionString = "hello, this is description speaking";
        RequestBody description =
                RequestBody.create(
                        MediaType.parse("multipart/form-data"), descriptionString);


        call = userRequest.uploadPhoto(description, body, authData);

    }

这篇关于无法从改造2中发布多部分数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆