Java 改装2.0“投掷”;IllegalArgumentException:@字段参数只能与表单编码“一起使用”;。如何进行正确的API查询并修复它?
我的问题是,我不知道如何开始对收到的API进行改装2.0-如下所述 首先,我需要输入用户名、密码、fbID(可选)、Gmail(可选)、twitID(可选)、性别、出生日期、位置(不需要-如果long和lat有值)、经度(可选)、纬度(可选)、轮廓图像(可选) 当所有参数正常时-接收Java 改装2.0“投掷”;IllegalArgumentException:@字段参数只能与表单编码“一起使用”;。如何进行正确的API查询并修复它?,java,android,api,retrofit,retrofit2,Java,Android,Api,Retrofit,Retrofit2,我的问题是,我不知道如何开始对收到的API进行改装2.0-如下所述 首先,我需要输入用户名、密码、fbID(可选)、Gmail(可选)、twitID(可选)、性别、出生日期、位置(不需要-如果long和lat有值)、经度(可选)、纬度(可选)、轮廓图像(可选) 当所有参数正常时-接收状态=真。 如果没有-接收status=false和错误的所需参数(例如邮件已被接收) 这样我就可以收到 status=true 或 status=false和最多5个参数(用户名、电子邮件、密码、性别、生日)的数组
状态=真
。
如果没有-接收status=false
和错误的所需参数(例如邮件已被接收)
这样我就可以收到
status=true
或
status=false
和最多5个参数(用户名、电子邮件、密码、性别、生日)的数组
我尝试了这个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;
}
}
}
我有错误:java.lang.IllegalArgumentException:@字段参数只能与表单编码一起使用。方法AuthRegisterUserApi.getStatus的(参数#1)
我尝试使用Postman注册用户,当我使用optionBody
->x-www-form-urlencoded
时,它就起作用了
如何创建此API的Iregister
查询?将@Field
更改为其他内容?我总是犯这个错误
编辑:需要将API接口
更改为此:
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;
}
}
}
公共接口AuthRegisterUserApi{
@FormUrlEncoded
@PUT(“/api/寄存器”)
调用getStatus(
@字段(“用户名”)字符串用户名,
@字段(“电子邮件”)字符串电子邮件,
@字段(“密码”)字符串密码,
@字段(“fbID”)字符串fbID,
@字段(“Gmail”)字符串Gmail,
@字段(“twitID”)字符串twitID,
@字段(“性别”)字符串性别,
@字段(“生日”)字符串生日,
@字段(“位置”)字符串位置,
@字段(“经度”)字符串经度,
@字段(“纬度”)字符串纬度,
@字段(“profileImage”)字符串profileImage
);
阶级工厂{
私有静态AuthRegisterUserApi服务;
公共静态AuthRegisterUserApi getInstance(){
改装改装=新改装.Builder()
.baseUrl(ApiConstants.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
服务=翻新.create(AuthRegisterUserApi.class);
回程服务;
}
}
}
BASE\u URL=http://ip_address:8400
对我来说
但仍然得到错误:
Response.rawResponse=Response{protocol=http/1.1,code=400,message=Bad请求,url=http://ip_address:8400/api/register}
。使用邮递员与我收到的相同数据创建代码=201不知道为什么…您的请求编码不正确,但是邮递员,所以请更改:
@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);
@FormUrlEncoded
@PUT(“/api/寄存器”)
调用getStatus(
@字段(“用户名”)字符串用户名,
@字段(“电子邮件”)字符串电子邮件,
@字段(“密码”)字符串密码,
@字段(“fbID”)字符串fbID,
@字段(“Gmail”)字符串Gmail,
@字段(“twitID”)字符串twitID,
@字段(“性别”)字符串性别,
@字段(“生日”)字符串生日,
@字段(“位置”)字符串位置,
@字段(“经度”)字符串经度,
@字段(“纬度”)字符串纬度,
@字段(“profileImage”)字符串profileImage);
告诉我是否可以。问题是因为我试图
放置
,例如经度\纬度\位置没有值-空字符串
我是说,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
);
@FormUrlEncoded
@PUT(ApiConstants.REGISTER\u URL\u部分)
呼叫登记器(
@字段(“用户名”)字符串用户名,
@字段(“电子邮件”)字符串电子邮件,
@字段(“密码”)字符串密码,
@字段(“fbID”)字符串fbID,
@字段(“Gmail”)字符串Gmail,
@字段(“twitID”)字符串twitID,
@字段(“性别”)字符串性别,
@字段(“生日”)字符串生日,
@可为空@字段(“位置”)字符串位置,
@可空@字段(“经度”)字符串经度,
@可为空@字段(“纬度”)字符串纬度,
@字段(“profileImage”)字符串profileImage
);
现在,当其中一个没有值时,我只是没有把这个字段括起来。我收到了一条类似的错误消息,我注意到在我的接口定义中,我遗漏了@ForUrlEncoded注释-在将使用表单部分的rest调用之上,这似乎与您在第一个发布的代码块中所做的相同。看一看
希望这有帮助。您刚刚错过了Rest方法调用上方的@FormUrlEncoded
。尝试将@Field
更改为@Query
@RaphaelTeyssandierIllegalArgumentException:缺少@PUT URL或@URL参数。对于方法AuthRegisterUserApi.getStatus
和myREGISTER\u URL=BASE\u URL+“REGISTER/”代码>我可以查看所有URL吗?您可以尝试保留@Field
并在上面添加@PUT
,@FormUrlEncoded
基本URL=http://ip_address:8400/api/
@RaphaelTeyssandier发现了相同的错误(缺少@PUT URL或@URL参数。对于方法AuthRegisterUserApi.getStatus
)使用@Field
和@FormUrlEncoded
在@PUT
Response.rawResponse=Response{protocol=http/1.1,code=400,message=Bad Request,url=http://ip_address:8400/api/register}
。Co这不起作用。您确定您的请求是put而不是post吗?是否尝试使用其他参数?因为自从你把他放进数据里
@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
);