使用改造2上传多张照片

时间:2018-06-11 07:16:42

标签: android retrofit retrofit2 multipartform-data multipart

我正在进行演示,用户可以点击多张照片并上传到服务器,这样我创建了一个列表,我可以存储所有图像但是在点击API结束时我得到了异常。 经历了multiple image upload using retrofit 2但无法解决问题。

这是我的日志猫:

java.lang.IllegalArgumentException: @Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #1)
        for method RetrofitInterface.uploadImages
        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
        at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:729)
        at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:592)
        at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:333)
        at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:202)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
        at $Proxy0.uploadImages(Native Method)
        at com.example.innobles.imagesuploader.Remote.RetrofitClient.uploads(RetrofitClient.java:41)

RetrofitClient.java:

public class RetrofitClient {

    public static final String BASE_URL = "http://example.com/abc/application/";
    private static RetrofitClient retrofitClient;
    private static RetrofitInterface retrofitInterface;

    public static RetrofitInterface getRetrofitApi() {
        //return retrofit object
    }

    public static Call<ServerResponse> uploads( Map<String,MultipartBody.Part> images) {
        return getRetrofitApi().uploadImages(images);
    }
}

RetrofitInterface .java

public interface RetrofitInterface {
    @Multipart
    @POST("upload_images.php")
    Call<ServerResponse> uploadImages(@Part Map<String,MultipartBody.Part> images);
}

uploadImage方法:

private void uploadImages(List<Uri> uriPaths) {
        Map<String,MultipartBody.Part> list = new HashMap<>();
        for (Uri uri : uriPaths) {
            MultipartBody.Part imageRequest = prepareFilePart("file[]", uri);
            list.put("images", imageRequest);
        }

        RetrofitClient.uploads(list).enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                if (response.isSuccessful() && response.body()!=null)
                    Log.e("MAIN", response.body().getMessage());

            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {
                Log.e("main", "on error is called and the error is  ----> " + t.getMessage());

            }
        });


    }

帮助方法:

  @NonNull
    private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
        File file = new File(fileUri.getPath());
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

        // MultipartBody.Part is used to send also the actual file name
        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }
我在做错了吗?请帮忙,我是新手。

1 个答案:

答案 0 :(得分:0)

我确实解决了这样的问题:

RetrfitClient.java 中添加 createRequestBody()方法,并在 uploadsImages(List uri)方法中调用此方法。

在RetrofitInterface.java中:

Call<ServerResponse> uploadImages(@Part Map<String,MultipartBody.Part> images);替换为Call<ServerResponse> uploadImages( @PartMap Map<String, RequestBody> files);

createRequest方法:

public static RequestBody createRequestBody(@NonNull File file){
        return RequestBody.create(MediaType.parse("multipart/form-data"),file);
    }