【问题标题】:Retrofit - throwing an exception java.lang.IllegalArgumentException: Only one encoding annotation is allowed改造 - 抛出异常 java.lang.IllegalArgumentException: 只允许一个编码注释
【发布时间】:2017-03-29 05:15:04
【问题描述】:

大家好,这里是我的示例代码

@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,它会抛出错误没有办法在 Retrofit 中同时使用 @Field 和 @part 吗??
- 如果是,请告诉我一个原因,如果不是,请告诉我正确的方法

感谢您的回答并提前感谢您

注意:投票前请在 cmets 中告诉我一个建议。

【问题讨论】:

  • 去掉@FormUrlEncoded注解.....
  • @sushildlh 我试过如果我删除@FormUrlEncoded 而不是得到这个>> java.lang.IllegalArgumentException: @Field 参数只能与表单编码一起使用。
  • 使用@Part 而不是@Field ..........

标签: android retrofit


【解决方案1】:

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

@FormUrlEncoded(适用于安卓)| application/x-www-form-urlencoded(用于网络)

@Multipart(适用于安卓)| multipart/form-data(用于网络)

这样使用.....

  @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,如果您卡在任何地方,请告诉我。

更多详情请点击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);

并像这样使用改造调用.....

 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

【讨论】:

  • 你的意思是说我需要创建另一个api仅用于图像上传我无法上传和传递带有字符串参数的文件:(对吗??
  • 您可以使用RequestBody 变量传递并将您的字符串放入其中...。在上面的示例中tokenRequestString .....
  • 谢谢我得到了我想要的,非常感谢您的宝贵时间再次感谢您:)
猜你喜欢
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 2022-07-31
  • 2022-09-30
相关资源
最近更新 更多