2017-11-17 26 views
0

문제가 있습니다. 응답을 반환하는 로그인 프로세스를 만들고 있습니다. 로그인이 맞으면 다음과 같은 응답을 보냅니다.리포지토리 패턴을 사용하여 MVP에서 응답 반환 catch

{ 
    "error": false, "message": "Logged In Successfully", 
    "user": { 
     "username": "administrator", 
     "email": "[email protected]", 
     "first_name": "USER", 
     "last_name": "ADMIN" 
    }, 
    "menu": [ 
     { 
     "name": "MENU A", 
     "module": "menu_a", 
     "controller": "", 
     "parent_module": "", 
     "status": "" 
     }, 
     { 
      "name": "MENU B", 
      "module": "menu_b", 
      "controller": " ", 
      "parent_module": "", 
      "status": "" 
     } 
    ], 
    "privilege": [ 
     { 
      "module": "menu_a" 
     }, 
     { 
      "module": "menu_b" 
     } 
    ] 
} 

그런 다음 false로 설정하면 다음과 같은 응답이 반환됩니다. 나는 토스트에 표시 응답이 거짓 이름을 테스트있을 때

public interface AppDataSource { 

    Single<LoginResponse> attemptLogin(Login login); 

    Single<LoginResponse> saveUserData(LoginResponse response); 

} 

public class AppRemoteDataSource implements AppDataSource { 

    private final Retrofit retrofit; 

    @Inject 
    public AppRemoteDataSource(@NonNull Retrofit retrofit) { 
     this.retrofit = retrofit; 
    } 

    @Override 
    public Single<LoginResponse> attemptLogin(Login login) { 
     return retrofit.create(OmrService.class).loginUser(login); 
    } 

    @Override 
    public Single<Boolean> checkLogin() { 
     throw new UnsupportedOperationException(); 
    } 
} 

public class AppLocalDataSource implements AppDataSource { 

    @NonNull 
    private final SharedPreferences sharedPreferences; 

    @NonNull 
    private final Realm realm; 

    @Inject 
    public AppLocalDataSource(@NonNull SharedPreferences sharedPreferences, @NonNull Realm realm) { 
     this.sharedPreferences = sharedPreferences; 
     this.realm = realm; 
    } 
    @Override 
    public Single<Boolean> checkLogin() { 
     return Single.create(s -> { 
      Boolean stsLogin = sharedPreferences.getBoolean("IS_USER_LOGIN", >false); 
      s.onSuccess(stsLogin); 
     }); 
    } 
    @Override 
    public Single<LoginResponse> saveUserData(LoginResponse response) { 
     return Single.create(e -> { 
      if(!response.isError()) { 
       savePreference(response.getUser()); 
       saveMenuPrivilege(response.getMenu(), >response.getPrivilege()); 
      } 
      e.onSuccess(response); 
     }); 
    } 

} 

public class AppRepository implements AppDataSource { 

    @NonNull 
    private final AppDataSource localDataSource; 

    @NonNull 
    private final AppDataSource remoteDataSource; 

    @Inject 
    public AppRepository(@NonNull @Local AppDataSource localDataSource, 
         @NonNull @Remote AppDataSource remoteDataSource) { 
     this.localDataSource = localDataSource; 
     this.remoteDataSource = remoteDataSource; 
    } 

    @Override 
    public Single<LoginResponse> attemptLogin(Login login) { 
     return remoteDataSource.attemptLogin(login) 
       .compose(RxUtils.applySingleSchedulers()) 
       .flatMap(localDataSource::saveUserData); 
    } 

    @Override 
    public Single<Boolean> checkLogin() { 
     return localDataSource.checkLogin(); 
    } 

} 

public class LoginPresenter implements LoginContract.Presenter { 


    private LoginContract.View view; 

    private CompositeDisposable compositeDisposable = new CompositeDisposable(); 

    private AppRepository repository; 


    @Inject 
    public LoginPresenter(AppRepository repository, LoginContract.View view) { 
     this.repository = repository; 
     this.view = view; 

    } 

    private void attemptLogin(Login login) { 
     view.showProgressIndicator(true); 
     compositeDisposable.add(repository.attemptLogin(login) 
       .subscribe(
         response -> { 
          if(response.isError()) { 
           Log.d("CHECK RESPONSE ", response.getMessage()); 
           view.makeSnack(response.getMessage().toString().replace("<p>","").replace("</p>","")); 
          } else { 
           view.showProgressIndicator(false); 
           view.startMainActivity(); 
          } 
         } , e -> { 
          Log.d("CHECK RESPONSE ERROR", e.getMessage()); 
          view.makeSnack(e.getMessage().toString()); 
          view.showProgressIndicator(false); 
         } 
       )); 
    } 
} 

문제가되지 않는 것입니다 "잘못된 로그인"대신 "com.google :

{ "error": true, "message": "Incorrect Login", "user": { 
    "username": "none", 
    "email": "none", 
    "first_name": "none", 
    "last_name": "none", 
    "company": "none", 
    "phone": "none", 
    "gender": "none", 
    "place_of_birth": "none", 
    "date_of_birth": "none", 
    "religion": "none", 
    "photo": "none" }, "menu": "none", "privilege": "none" } 

이 내 코드입니다. gson.JsonSyntaxException : java.lang.IllegalStateException : 예상 된 BEGIN_ARRAY하지만 줄 1 열에서 열 STRING 않았습니다. 268 경로 $ .menu "

로그인이 잘못된 경우 메뉴와 권한이 비어 있음을 알고 있습니다. 그러나 내가 어떻게 "잘못된 로그인"응답 메시지를 잡을 수 있습니까?, 내가 변경해야 할 코드의 부분이 있습니까? 사전

답변

1

문제는 당신 privilege 핵심에

감사합니다. "권한": "없음"

"none"-[]

을 변경하는 것이 될 것이다 작동하게하는 간단한 방법 실패 "privilege": [ { "module": "menu_a" }, { "module": "menu_b" } ]

이 문자열은 다음과 같습니다 성공

그것은 배열입니다

문제는 JSON 디시리얼라이저 (GSON?)에 있으며 이는 분명히 String과 Array를 동일한 방식으로 나타낼 수 없습니다.

서버 응답을 변경할 수없는 경우 변환을 수행 할 수있는 사용자 지정 데시리아 화 프로그램을 만들어야 할 가능성이 큽니다. 이를 수행하는 방법은 JSON을 Java로 변환하는 데 사용되는 사항에 따라 다릅니다.

+0

고마워, 내 문제를 해결해. 지금은 []를 사용할 것이고, 응답을 변경하는 것도 좋은 생각입니다. 고마워 btw, –