改进-抛出异常java.lang.IllegalArgumentException:仅允许一个编码注释 [英] Retrofit - throwing an exception java.lang.IllegalArgumentException: Only one encoding annotation is allowed

查看:599
本文介绍了改进-抛出异常java.lang.IllegalArgumentException:仅允许一个编码注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是我的示例代码

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);

问题是,当我尝试将文件参数添加为零件时,如果我仅使用@Field,则会向我抛出错误,这很好,但在我将@Part添加到其中后却无法正常工作
-在翻新中无法同时使用@Field和@part吗?
-如果可以,则不能告诉我一个正确的方法

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.

推荐答案

不能在单个方法上同时使用@FormUrlEncoded和@Multipart. 一个HTTP请求只能有一个Content-Type,而这两个都是 内容类型.

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 (适用于android)| 应用程序/x-www-form-urlencoded (用于网络)

@FormUrlEncoded (for android) | application/x-www-form-urlencoded(for web)

@Multipart (适用于android)| 多部分/表单数据(用于网络)

@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 );

在通话中.....

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();
            }
        });
    }

注意:-如果您在任何地方卡住,请使用以上示例进行文件POST,并告诉我.

有关更多详细信息,请单击

For more Detail info click this

-

对于您的情况,使用这种方式.....

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();
                }
            });
        }

示例

这篇关于改进-抛出异常java.lang.IllegalArgumentException:仅允许一个编码注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆