2017-11-17 4 views
0

私は問題に直面しています:私はレスポンスを返すログインプロセスを作成しています。ログインが真であれば、このような応答を出します。リポジトリパターンでMVPでレスポンスリターンをキャッチ

{ 
    "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 path $ .menu "

ログインが偽の場合、メニューと権限は空であることがわかります。しかし、私はどのように応答メッセージ "不正なログイン"をキャッチすることができますか?、私は変更する必要があるコードの一部ですか?問題は、あなたのprivilege鍵となり、事前

答えて

1

感謝。 「特権」:「なし」

"none"から[]

を変更するには、それは次のようになり動作させるための簡単な方法失敗に "privilege": [ { "module": "menu_a" }, { "module": "menu_b" } ]

それが文字列である:成功し

は、それが配列であります

問題はJSONデシリアライザ(GSON?)にあります。これは、明らかにStringとArrayの両方を同じ方法で表すことはできません。

サーバーの応答を変更できない場合は、変換を実行できるカスタムデシリアライザを作成する必要があります。どのようにするかは、JSONをJavaに変換するために使用されているものによって異なります。

+0

ありがとう、私の問題を解決します。今のところ私は[]を使用しますが、応答も変わる可能性があります。ありがとうbtw、 –

関連する問題