Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit - throwing an exception java.lang.IllegalArgumentException: Only one encoding annotation is allowed

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.

like image 896
Kishan Soni Avatar asked Nov 15 '16 10:11

Kishan Soni


People also ask

What is illegalargumentexception in Java?

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.

Is it possible to use multiple encoding annotations in retrofit?

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.

Does illegalargumentexception need to be declared in the throws clause?

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.

How many encoding annotations can I use with the @multipart encoding?

and I get an IllegalArgumentException and only one encoding annotation is allowed. You can use two @Part parameters with the @Multipart encoding.


1 Answers

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

like image 164
sushildlh Avatar answered Oct 06 '22 10:10

sushildlh