使用电子邮件和密码进行改造登录
问题描述:
我遇到登录问题。我尝试了几个来自网络的例子,但没有为我工作。使用电子邮件和密码进行改造登录
JSON
{
"responseCode":1,
"responseCodeText":"ok",
"response":
{
"id":1234,
"email":"[email protected]",
"name":"name",
"lastname":"lastname",
"properties":[
//...
],
"rights":"admin",
"photo":"url",
"favorites":
[
//...
],
"token":"token"
}
}
我已经创建模型类从http://www.jsonschema2pojo.org/,所以它应该是确定。
接口
public interface LoginInterface {
@POST("login/{email}/{password}")
Call<UserResponse> login(@Path("email") String email, @Path("password") String password);
}
呼叫在活动
public void serviceInit() {
String email = edEmail.getText().toString();
String password = edPassword.getText().toString();
FactoryAPI.getInstanceLogin().login(email, password).enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if(response.isSuccessful()) {
Intent intent = new Intent(getContext(), AccountActivity.class);
startActivity(intent);
}
//TODO: load from sharedPreferences
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
Log.e("error", "points not loaded");
}
});
}
错误,我有
responseCodeText: “属性错误”
我收到了来自服务器的请求。我的电子邮件地址和密码是GET,不POST
感谢您的帮助
答
您回应类的JSON将被映射到必须有确切的结构和属性的JSON。
所以你的UserResponse应该包括所有的字段。事情是这样的:
public class UserResponseDTO {
public int responseCode;
public String responseCodeText;
public UserDTO response;
}
public class UserDTO {
public int id;
public String email;
public String name;
public String lastname;
public List<PropertyDTO> properties;
public String rights;
public String photo;
public List<FavoriteDTO> favorites;
public String token;
}
答
的问题是有:
@FormUrlEncoded
@POST("login")
Call<UserResponse> login(@Field("email") String email, @Field("password") String password);
,你得到什么错误? –
你面临什么样的麻烦? –
请修改您的问题以包含错误的描述。 – theFunkyEngineer