我的问题是 我不知道如何开始使用 Retrofit 2.0 和收到的 API - 如下所述……
首先,我需要用户名、密码、fbID(可选)、gmailID(可选)、twitID(可选)、性别、出生日期、位置(不需要 - 如果 long 和 lat 有值)、经度(可选)、纬度(可选) , 个人资料图片(可选)。
当所有参数都正确时 - 接收 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;
}
}
}
这段代码在 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 许可协议
The problem was because I try to
PUT
eglongitude
\latitude
\location
with no value - emptyString
.我的意思是 - API 方面有问题。所以为了避免我将方法更改为:
现在,当我对其中一个没有价值时,我只是不包含这个字段。