2016-08-23 94 views
0

我遇到登录问题。我尝试了几个来自网络的例子,但没有为我工作。使用电子邮件和密码进行改造登录

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

感谢您的帮助

+0

,你得到什么错误? –

+0

你面临什么样的麻烦? –

+0

请修改您的问题以包含错误的描述。 – theFunkyEngineer

回答

0

您回应类的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; 
} 
+0

我发现问题出现在@ FormUrlEncoded中。可能吗?那就是我添加的,现在它正在工作。 – Stepan

+0

所以你没有像'@POST(“login/{email}/{password}”)那样指定端点? 您是否介意使用工作解决方案编辑您的文章? – raxelsson

0

的问题是有:

@FormUrlEncoded 
@POST("login") 
Call<UserResponse> login(@Field("email") String email, @Field("password") String password); 
相关问题