Retrofit 2.0 抛出“IllegalArgumentException:@Field 参数只能与表单编码一起使用”。如何进行正确的 API 查询并修复它?

新手上路,请多包涵

我的问题是 我不知道如何开始使用 Retrofit 2.0 和收到的 API - 如下所述……

首先,我需要用户名、密码、fbID(可选)、gmailID(可选)、twitID(可选)、性别、出生日期、位置(不需要 - 如果 long 和 lat 有值)、经度(可选)、纬度(可选) , 个人资料图片(可选)。

当所有参数都正确时 - 接收 status = true 。如果不是 - 接收 status = false 和错误的必需参数(例如邮件已被占用)

所以我可以接收 status = truestatus = false 和最多 5 个参数(用户名、电子邮件、密码、性别、出生日期)的数组。

我试过这个 API Interface

 public interface AuthRegisterUserApi {
    @PUT()
    Call<AuthRegisterUserModel> getStatus(
            @Field("username") String username,
            @Field("email") String email,
            @Field("password") String password,
            @Field("fbID") String fbID,
            @Field("gmailID") String gmailID,
            @Field("twitID") String twitID,
            @Field("gender") String gender,
            @Field("birthDate") String birthDate,
            @Field("location") String location,
            @Field("longitude") String longitude,
            @Field("latitude") String latitude,
            @Field("profileImage") String profileImage
            );

    class Factory {
        private static AuthRegisterUserApi service;

        public static AuthRegisterUserApi getInstance() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.REGISTER_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            service = retrofit.create(AuthRegisterUserApi.class);

            return service;
        }
    }
}

使用此 API 模型 (Pastebin.com)

这段代码在 Activity 中:

 AuthRegisterUserApi.Factory.getInstance()
        .getStatus(
                usernameEditText.getText().toString(),
                emailEditText.getText().toString(),
                passwordEditText.getText().toString(),
                "", "", "",
                (maleRadioButton.isChecked() ? "male" : "female"),
                mYear + "-" + mMonth+1 + "-" + mDay,
                (geoLocationToggleButton.isChecked() ? geoLocationEditText.getText().toString() : ""),
                (!geoLocationToggleButton.isChecked() ? "50" : ""),
                (!geoLocationToggleButton.isChecked() ? "50" : ""),
                "")
        .enqueue(new Callback<AuthRegisterUserModel>() {
    @Override
    public void onResponse(Call<AuthRegisterUserModel> call, Response<AuthRegisterUserModel> response) {
        if(response.isSuccessful()) {
            if (response.body().isStatus()) {
                showToast(getApplicationContext(), "Registration ok.");
            } else {
                response.body().getInfo().getUsername();
            }
        }
    }

    @Override
    public void onFailure(Call<AuthRegisterUserModel> call, Throwable t) {

    }
});

我有错误: java.lang.IllegalArgumentException: @Field parameters can only be used with form encoding. (parameter #1) for method AuthRegisterUserApi.getStatus

我尝试使用 Postman 注册用户,当我使用选项 Body -> x-www-form-urlencoded 时它起作用了。

我如何创建 I register 对此 API 的查询?@Field 更改为其他内容?我总是遇到这个错误……

编辑:需要将 API Interface 更改为

 public interface AuthRegisterUserApi {
    @FormUrlEncoded
    @PUT("/api/register")
    Call<AuthRegisterUserModel> getStatus(
            @Field("username") String username,
            @Field("email") String email,
            @Field("password") String password,
            @Field("fbID") String fbID,
            @Field("gmailID") String gmailID,
            @Field("twitID") String twitID,
            @Field("gender") String gender,
            @Field("birthDate") String birthDate,
            @Field("location") String location,
            @Field("longitude") String longitude,
            @Field("latitude") String latitude,
            @Field("profileImage") String profileImage
            );

    class Factory {
        private static AuthRegisterUserApi service;

        public static AuthRegisterUserApi getInstance() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            service = retrofit.create(AuthRegisterUserApi.class);

            return service;
        }
    }
}

BASE_URL = http://ip_address:8400 对我来说……

但仍然出现错误: Response.rawResponse = Response{protocol=http/1.1, code=400, message=Bad Request, url=http://ip_address:8400/api/register} 。将 Postman 与我收到的相同数据一起使用,code=201 已创建。 不知道为什么…

原文由 y07k2 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 614
2 个回答

The problem was because I try to PUT eg longitude \ latitude \ location with no value - empty String .

我的意思是 - API 方面有问题。所以为了避免我将方法更改为:

 @FormUrlEncoded
@PUT(ApiConstants.REGISTER_URL_PART)
Call<RegisterModel> registerUser(
        @Field("username") String username,
        @Field("email") String email,
        @Field("password") String password,
        @Field("fbID") String fbID,
        @Field("gmailID") String gmailID,
        @Field("twitID") String twitID,
        @Field("gender") String gender,
        @Field("birthDate") String birthDate,
        @Nullable @Field("location") String location,
        @Nullable @Field("longitude") String longitude,
        @Nullable @Field("latitude") String latitude,
        @Field("profileImage") String profileImage
);

现在,当我对其中一个没有价值时,我只是不包含这个字段。

原文由 y07k2 发布,翻译遵循 CC BY-SA 3.0 许可协议

你刚刚错过了 @FormUrlEncoded 在你的 Rest 方法调用之上。

原文由 Rajesh.k 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏