在我们的网络服务中,我们可以使用以下结构传递用户名和密码,我们可以成功地从服务器获取数据
POST -> http://www.sample.com/Service1.svc/login
Content-Type: application/json
Body:
{
"UserName": "1",
"Password": "1"
}
或带卷曲:
curl -X POST -H 'Content-Type: application/json' -i 'http://www.sample.com/Service1.svc/login' --data '{
"UserName": "1",
"Password": "1"
}'
我对Retrofit
的问题是用户名和密码上的第一个字符的大写,当我实现下面的代码时,我得到0
因为用户名和密码不正确
LoginSchema
类:
class LoginSchema {
private int Username;
private int Password;
public LoginSchema(int username, int password) {
Username = username;
Password = password;
}
}
Retrofit
实现:
LoginSchema loginSchema = new LoginSchema(1, 1);
Call<Integer> call = requestService.checkLoginAccount(loginSchema);
call.enqueue(new Callback<Integer>() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onResponse(@NotNull Call<Integer> call, @NotNull final Response<Integer> response) {
try {
if (response.isSuccessful() && response.body() != null) {
EventBus.getDefault().post(new EventLoginResult(true, response.body().toString()));
} else {
EventBus.getDefault().post(new EventLoginResult(false, null));
}
} catch (Exception e) {
EventBus.getDefault().post(new EventLoginResult(false, null));
}
}
@Override
public void onFailure(Call<Integer> call, Throwable t) {
}
});
然后是 API 实现:
public interface RestfulWebServices {
@Headers("Content-Type: application/json")
@POST("/Service1.svc/login")
Call<Integer> checkLoginAccount(@Body TaskLogin.LoginSchema loginSchema);
}
你的 POJO 类应该如下所示。
class LoginSchema {
@SerializedName("UserName")
private int Username;
@SerializedName("Password")
private int Password;
public LoginSchema(int username, int password) {
Username = username;
Password = password;
}
}