【问题标题】:Sending Multipart data using Retrofit使用 Retrofit 发送多部分数据
【发布时间】:2017-08-01 17:30:49
【问题描述】:

界面

public interface iUpload{
        @Multipart
        @POST("/uploadmultiplepropimages/")
         SamplePojoClass getUploadData(
            @Part("prop_id") RequestBody prop_id,
            @Part("type") RequestBody type,
            @Part("prop_photos") TypedFile prop_photos
        );
}

我是这样发送的。我不能像这样发送请求正文。

@Override

    protected Void doInBackground(String... params) {

        String s = params[0];
        File photoFile = new File(s);
        System.out.println("file path:"+photoFile);
        TypedFile photoTypedFile = new TypedFile("image/png", photoFile);

        RequestBody idd = RequestBody.create(MediaType.parse("text/plain"), "");
        RequestBody type = RequestBody.create(MediaType.parse("text/plain"), "single");

        try {
            //uploadImageResponse = RequestResponse.getUploadData(AccountUtils.getProfileId(),photoTypedFile);
            uploadImageResponse = RequestResponse.getUploadData(idd,type,photoTypedFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }`

它说错误:

无法访问 ByteString 类文件。

【问题讨论】:

    标签: android retrofit retrofit2 multipart


    【解决方案1】:

    我希望你在你的 gradle 文件中添加了 okio 依赖。这将解决 无法访问 ByteString 类文件错误。

    compile 'com.squareup.okio:okio:1.13.0'
    

    然后编辑您的iUpload 接口文件,如:

    public interface iUpload{
            @Multipart
            @POST("/uploadmultiplepropimages/")
            SamplePojoClass getUploadData(
                    @Part MultipartBody.Part file
                    @Part MultipartBody.Part prop_id,
                    @Part MultipartBody.Part type
            );
        }
    

    然后像这样写MultipartBody.Part

    RequestBody lRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), pFile);
         MultipartBody.Part lFile = MultipartBody.Part.createFormData("file", pFile.getName(), lRequestBody);
         MultipartBody.Part id = MultipartBody.Part.createFormData("prop_id", "WRITE_ID_HERE");
         MultipartBody.Part type = MultipartBody.Part.createFormData("type", "WRITE TYPE HERE");
    

    最后将这些参数传递给您的 api,如下所示:

    uploadImageResponse = RequestResponse.getUploadData(lFile,id,type);
    

    希望它能解决你的问题。

    注意:这里的pFileFile 的实例。要从目录中获取文件,您可以编写如下代码:

    File pFile = new File("PATH_OF_FILE");
    

    【讨论】:

    • 不工作它给出以下错误 {"headers":{"namesAndValues":["Content-Disposition","form-data; name\u003d\"prop_id\""]}} {" headers":{"namesAndValues":["Content-Disposition","form-data; name\u003d\"type\""]}}
    • @LaxmanMarth 请发布您获得的整个错误日志。并将其更新为您的问题。
    【解决方案2】:
    I have done Multipart upload in okhttp. I hope this will help.
            MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
            multipartBuilder.setType(MultipartBody.FORM);
            multipartBuilder.addFormDataPart("prop_photos", photoFile, RequestBody.create(MEDIA_TYPE_PNG, file));
    
                multipartBuilder
                             .addFormDataPart("type", type)
                            .addFormDataPart("prop_id", prop_id);
    
        RequestBody requestBody = multipartBuilder.build();
        Request request1 = new Request.Builder().url(urlString).post(requestBody).build();
    

    【讨论】:

      【解决方案3】:

      使用以下函数解决您的问题

      public static RegisterResponse Uploadimage(String id, String photo,String proof) {
      
              File file1 = null, file2 = null;
              try {
                  if (photo.trim() != null && !photo.trim().equals("")) {
                      file1 = new File(photo);
                  }
                  if (proof.trim() != null && !proof.trim().equals("")) {
                      file2 = new File(proof);
                  }
      
                  HttpClient client = new DefaultHttpClient();
                  HttpPost post = new HttpPost(WEB_SERVICE + "protoupload.php?");
      
                  MultipartEntity reqEntity = new MultipartEntity();
      
                  // reqEntity.addPart("studentid", new StringBody(
                  // Global.profileDetail.id));
                  reqEntity.addPart("id", new StringBody(id));
      
                  if (file1 == null) {
                      reqEntity.addPart("photo", new StringBody(""));
                  } else {
                      FileBody bin1 = new FileBody(file1);
                      reqEntity.addPart("photo", bin1);
                  }
                  if (file2 == null) {
                      reqEntity.addPart("proof", new StringBody(""));
                  } else {
                      FileBody bin2 = new FileBody(file2);
                      reqEntity.addPart("proof", bin2);
                  }
                  post.setEntity(reqEntity);
                  HttpResponse response = client.execute(post);
                  HttpEntity resEntity = response.getEntity();
                  String inputStreamString = EntityUtils.toString(resEntity);
      
                  if (inputStreamString.contains("result")) {
      
                      return new Gson().fromJson(inputStreamString,
                              RegisterResponse.class);
                  }
      
              } catch (Exception ex) {
                  Log.e("Debug", "error: " + ex.getMessage(), ex);
              }
      
              return new RegisterResponse();
          }
      

      【讨论】:

        猜你喜欢
        • 2015-06-23
        • 2014-09-18
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-12
        • 2016-09-09
        • 2017-02-02
        相关资源
        最近更新 更多