【问题标题】:upload multiple photos using retrofit 2使用改造 2 上传多张照片
【发布时间】:2018-11-20 09:08:03
【问题描述】:

我正在开发演示,用户可以单击多张照片并上传到服务器,因此我创建了存储所有图像的列表,但在点击 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);
    }

我哪里做错了?请帮忙,我是android新手。

【问题讨论】:

标签: android retrofit retrofit2 multipartform-data multipart


【解决方案1】:

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

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

在 RetrofitInterface.java 中:

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

createRequest 方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多