Hello guys here is my sample code
@FormUrlEncoded
@Multipart
@POST("registration.php")
Call<Signup> getSignupResponse(@Field("email") String email,
@Field("lname") String lname,
@Field("fname") String fname,
@Field("password") String password,
@Part("filename") File file);
Issue is when i am trying to add File Parameter as a Part its throwing me a error other wise if i use only @Field it works great but not working after i add @Part in it
- is there a no way to use @Field and @part together in Retrofit??
- If yes than tell a reason, If no tell me a proper way
I will appreciate your answer and thank you in advance
Note : Tell me a suggestions in comments before you vote.
In this tutorial, we will discuss how to solve the java.lang.illegalargumentexception – IllegalArgumentException in Java. This exception is thrown in order to indicate that a method has been passed an illegal or inappropriate argument.
and I get an IllegalArgumentException and only one encoding annotation is allowed. You can use two @Part parameters with the @Multipart encoding. One for the form encoded parts and the other for the JSON body. Retrofit will run @Part s through the converters, so make sure you're using the appropriate parameter types.
Since IllegalArgumentException is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor. An IllegalArgumentException code> occurs when an argument passed to a method doesn't fit within the logic of the usage of the argument.
and I get an IllegalArgumentException and only one encoding annotation is allowed. You can use two @Part parameters with the @Multipart encoding.
You cannot use both @FormUrlEncoded and @Multipart on a single method. An HTTP request can only have one Content-Type and both of those are content types.
@FormUrlEncoded (for android) | application/x-www-form-urlencoded(for web)
@Multipart (for android) | multipart/form-data(for web)
use like this .....
@Multipart
@POST("photos/upload")
Call<Result> upload(@Part("Token") RequestBody token, @Part("Photo_Type") RequestBody type, @Part MultipartBody.Part file );
and in call like this .....
String token="your string";
File file = new File(path);
RequestBody tokenRequest = RequestBody.create(MediaType.parse("text/plain"), token);
RequestBody type = RequestBody.create(MediaType.parse("text/plain"), true + "");
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
Call<Result> call = qikGrubApi.upload(tokenRequest, type, filePart);
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
progress.dismiss();
if (response.isSuccessful()) {
if (response.body().getSuccess()) {
nextPage(response.body().getMessage());
} else
Toast.makeText(UploadActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UploadActivity.this, "Sorry for inconvince server is down", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
progress.dismiss();
Toast.makeText(UploadActivity.this, "Check your Internet connection", Toast.LENGTH_SHORT).show();
}
});
}
Note:- Use above example for your File POST and let me know , if you stuck anywhere.
For more Detail info click this
EDIT:-
For your case use like this .....
@Multipart
@POST("registration.php")
Call<Signup> getSignupResponse(@Part("email") RequestBody email,
@Part("lname") RequestBody lname,
@Part("fname") RequestBody fname,
@Part("password") RequestBody password,
@Part MultipartBody.Part filename);
and use retrofit call like this .....
File file = new File(path);
RequestBody emailRequest = RequestBody.create(MediaType.parse("text/plain"), email);
RequestBody lnameRequest = RequestBody.create(MediaType.parse("text/plain"), lname);
RequestBody fnameRequest = RequestBody.create(MediaType.parse("text/plain"), fname);
RequestBody passwordRequest = RequestBody.create(MediaType.parse("text/plain"), password);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
Call<Signup> call = qikGrubApi.upload(emailRequest, lnameRequest ,fnameRequest , passwordRequest, filePart);
call.enqueue(new Callback<Signup>() {
@Override
public void onResponse(Call<Signup> call, Response<Signup> response) {
progress.dismiss();
if (response.isSuccessful()) {
if (response.body().getSuccess()) {
nextPage(response.body().getMessage());
} else
Toast.makeText(UploadActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UploadActivity.this, "Sorry for inconvince server is down", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Signup> call, Throwable t) {
progress.dismiss();
Toast.makeText(UploadActivity.this, "Check your Internet connection", Toast.LENGTH_SHORT).show();
}
});
}
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With