2016-08-23 92 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); 
相關問題