![](/img/trans.png)
[英]Android Retrofit: How to upload multiple images to server using retrofit?
[英]upload multiple photos using retrofit 2
我正在演示,用戶可以單擊多張照片並上傳到服務器,因此我創建了存儲所有圖像的列表,但在達到API終點時卻出現了異常。 使用翻新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新手。
我確實解決了這樣的問題:
在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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.