图片上传无法使用Multipart

时间:2017-06-01 11:26:41

标签: android android-studio http-post multipartentity

下面是请求我用邮递员尝试的参数并且工作正常

Postman request Parameter

但是,我无法从Android实际操作。

以下是我尝试的内容

File urls= new File(fn)

Part[] parts = new Part[1];
for (int i = 0; i < parts.length; i++) {
try {
      parts[i] = new FilePart("image", urls);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
}

MultipartEntity reqEntity = new MultipartEntity(parts,httpPost.getParams());
httpPost.setEntity(reqEntity);

会很感激解决方案

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Android Upload Service库。 我已经在我的组织的生产应用程序中多次使用过它。

要遵循的步骤(如Github Wiki所示) -

1)Setup

2)Multi-part Upload Example

以下是快速审核的方法 -

public void uploadMultipart(final Context context) {
    try {
        String uploadId =
          new MultipartUploadRequest(context, "http://upload.server.com/path")
            // starting from 3.1+, you can also use content:// URI string instead of absolute file
            .addFileToUpload("/absolute/path/to/your/file", "your-param-name")
            .setNotificationConfig(new UploadNotificationConfig())
            .setMaxRetries(2)
            .startUpload();
    } catch (Exception exc) {
        Log.e("AndroidUploadService", exc.getMessage(), exc);
    }
}