Reputation:
I'm working on demo where user can click multiple photo and upload into server so i created list where i'm storing all images but while hitting API end Point i'm getting Exception. gone through multiple image upload using retrofit 2 but unable to resolve issue.
here is my log cat :
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 method:
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());
}
});
}
Helper method:
@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);
}
where I'm doing wrong ? please help, I'm new to android.
Upvotes: 0
Views: 3872
Reputation:
I did solve ma problem like this:
In RetrfitClient.java add createRequestBody() method and call this method inside uploadsImages(List uri) method.
In RetrofitInterface.java:
replace Call<ServerResponse> uploadImages(@Part Map<String,MultipartBody.Part> images);
with Call<ServerResponse> uploadImages( @PartMap Map<String, RequestBody> files);
createRequest method :
public static RequestBody createRequestBody(@NonNull File file){
return RequestBody.create(MediaType.parse("multipart/form-data"),file);
}
Upvotes: 1