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

别来无恙 提交于 2019-12-17 20:12:09

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/40607862/retrofit-throwing-an-exception-java-lang-illegalargumentexception-only-one-en

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!