【问题标题】:Retrofit 2.0 throwing "IllegalArgumentException: @Field parameters can only be used with form encoding". How to do right API query and fix it?Retrofit 2.0 抛出“IllegalArgumentException:@Field 参数只能与表单编码一起使用”。如何进行正确的 API 查询并修复它?
【发布时间】:2016-10-25 22:42:23
【问题描述】:

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

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

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

所以我可以收到 status = true 要么 status = 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;
        }
    }
}

with this API models (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 Created。 不知道为什么...

【问题讨论】:

  • 尝试将@Field改为@Query
  • @RaphaelTeyssandier IllegalArgumentException: Missing either @PUT URL or @Url parameter. for method AuthRegisterUserApi.getStatus 和我的 REGISTER_URL = BASE_URL + "register/";
  • 我能看到所有的网址吗?你可以尝试保留@Field并在上面添加@PUT@FormUrlEncoded
  • BASE_URL = http://ip_address:8400/api/
  • @RaphaelTeyssandier 与@Field@FormUrlEncoded 以上@PUT 相同的错误(Missing either @PUT URL or @Url parameter. for method AuthRegisterUserApi.getStatus

标签: java android api retrofit retrofit2


【解决方案1】:

您刚刚在 Rest 方法调用上方错过了 @FormUrlEncoded

【讨论】:

    【解决方案2】:

    您的请求编码不正确,而是邮递员,所以请更改:

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

    告诉我是否可以。

    【讨论】:

    • Response.rawResponse = Response{protocol=http/1.1, code=400, message=Bad Request, url=http://ip_address:8400/api/register}。这不工作。
    • 您确定您的请求是 put 而不是 post ?
    • 尝试不同的参数?因为既然你把他放进数据库你就不能评价他?
    • 首先在应用程序上尝试不同的参数 - 响应为空;关于邮递员 - 回复status = true。心灵打击...
    • 好吧,这很奇怪,在邮递员上,端点是注册/或注册?
    【解决方案3】:

    问题是因为我尝试PUT 例如longitude \ latitude \ location 没有值 - 空 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
    );
    

    现在,当我对其中一个没有任何价值时,我根本不包含此字段。

    【讨论】:

      【解决方案4】:

      我收到了类似的错误消息,我注意到在我的接口定义中我错过了 @ForUrlEncoded 注释 - 在将使用表单部分的其余调用之上,这似乎与您在第一次发布时所做的相同代码块。看看吧。

      希望这会有所帮助。

      【讨论】:

      • 你救了我,伙计!!!非常感谢。我错误地删除了这一行并遇到了这个错误。
      猜你喜欢
      • 2015-02-03
      • 2016-08-26
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2019-08-04
      相关资源
      最近更新 更多