【发布时间】: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