微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Retrofit 2.0抛出“IllegalArgumentException:@Field参数只能用于表单编码”.如何做正确的API查询并修复它?

我的问题是,我不知道如何开始使用Retrofit 2.0与接收的API – 下面提到…

首先,我需要用户名,密码,fbID(可选),gmailID(可选),twitID(可选),性别,birthDate,位置(不需要 – 如果long和lat有值),经度(可选),纬度(可选),profileImage(可选).

当所有参数都很好 – 接收状态=真.
如果没有 – 接收状态= false,并且所需的参数错误(例如邮件已被占用)

所以我可以收到
status = true
要么
status = false和最多5个参数(用户名,电子邮件,birthDate)的数组.

我试过这个API接口:

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" : ""),"")
        .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参数只能与表单编码一起使用. (参数#1)用于方法AuthRegisterUserApi.getStatus

我尝试使用Postman注册用户,当我使用选项Body – >的X WWW窗体-urlencoded.

如何创建我的注册查询到这个API?将@Field更改为其他内容?我总是有这个错误

编辑:需要更改API接口为此:

public interface AuthRegisterUserApi {
    @FormUrlEncoded
    @PUT("/api/register")
    Call<AuthRegisterusermodel> getStatus(
            @Field("username") String username,@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.不知道为什么

解决方法

你的请求不是正确的编码,而是邮递员,所以改变:
@FormUrlEncoded
@PUT("/api/register")
    Call<AuthRegisterusermodel> getStatus(
            @Field("username") String username,@Field("profileImage") String profileImage);

告诉我,如果没关系

原文地址:https://www.jb51.cc/java/125121.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐