2017-01-04 83 views
2

我發送API請求到服務器,我可以得到兩個JSON響應:如何使用Retrofit和RxJa處理兩個JSON響應?

  • 用於身份驗證的用戶JSON響應
  • 爲未認證用戶JSON響應

驗證用戶響應:

{"status":"ok","tekst":"8bc6c9cf-293f-11e5-9940-448a5b5dd2bd","requestID":9034} 

未經身份驗證的用戶響應:

{"authenticationError":"User authentication failed"} 

改造服務:

public interface LiveService { 
    @GET("live.php") 
    Observable<Response<LiveResponse>> getLive(); 
} 

這是服務器改造要求:

retrofit.create(LiveService.class) 
      .getLive() 
      .subscribeOn(Schedulers.newThread()) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .subscribe(new Subscriber<Response<LiveResponse>>() { 
       @Override 
       public void onCompleted() { 

       } 

       @Override 
       public void onError(Throwable e) { 
        liveView.onLiveError(e); 
       } 

       @Override 
       public void onNext(Response<LiveResponse> response) { 


       } 
      }); 

我該如何處理在onNext響應檢查,如果我歌廳用於身份驗證的用戶或JSON響應爲未經驗證的用戶?

回答

3

我同意的StarWind,增加一個字段將幫助您稍後決定如何處理響應,但首先,你需要調整你的方法,其中最後的結果是不是Response<T>而是人體(物體)。使用RxJava可以通過以下方式鏈接調用來輕鬆完成此操作:接收響應,檢查它是否成功,檢查反序列化的body對象是否包含身份驗證錯誤字段。

retrofit.create(LiveService.class).getLive().flatMap(new Function<Response<LiveResponse>, ObservableSource<LiveResponse>>() { 
      @Override 
      public ObservableSource<LiveResponse> apply(Response<LiveResponse> liveResponse) throws Exception { 
       LiveResponse body = liveResponse.body(); 
       if(liveResponse.isSuccessful() && body != null && body.getAuthenticationError() == null){ //probably check http error code too 
        return Observable.just(body); 
       }else if(body != null){ 
        throw new AuthenticationException(body.getAuthenticationError()); 
       }else{ 
       throw IllegalArgumentException("something terribly happened here"); } 
      } 
     }).subscribeOn(Schedulers.newThread()) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .subscribe(new Observer<LiveResponse>() { 
       @Override 
       public void onComplete() { 

       } 

       @Override 
       public void onError(Throwable e) { 
        liveView.onLiveError(e); //authentication error maybe or network excp 
       } 

       @Override 
       public void onNext(LiveResponse body) { 


       } 
      }); 

請注意,我用RxJava 2,但你可以很容易地把它改寫在RxJava 1

4

您的模型需要將所有字段都合併爲一個POJO。那麼簡單:-)

未經驗證的用戶/未經驗證的用戶的驗證用戶將僅爲空。

要更清楚...

讓我們拿兩個不同的答覆。這是我遇到的兩個隨機JSON有效載荷。

{ 
     "title": "Sample Konfabulator Widget", 
     "name": "main_window", 
     "width": 500, 
     "height": 500 
    } 



    { 
     "src": "Images/Sun.png", 
     "name": "sun1", 
     "hOffset": 250, 
     "vOffset": 250, 
     "alignment": "center" 
    } 

帶領我們到一個組合對象像

{ 
    "title": "Sample Konfabulator Widget", 
    "name": "main_window", 
    "width": 500, 
    "height": 500, 
    "src": "ImagesSun.png", 
    "hOffset": 250, 
    "vOffset": 250, 
    "alignment": "center" 
    } 

給我們一個POJO看起來像這樣。

public class MyPojo 
{ 
    private String alignment; 

    private String title; 

    private String hOffset; 

    private String height; 

    private String width; 

    private String name; 

    private String src; 

    private String vOffset; 

    public String getAlignment() 
    { 
     return alignment; 
    } 

    public void setAlignment (String alignment) 
    { 
     this.alignment = alignment; 
    } 

    public String getTitle() 
    { 
     return title; 
    } 

    public void setTitle (String title) 
    { 
     this.title = title; 
    } 

    public String getHOffset() 
    { 
     return hOffset; 
    } 

    public void setHOffset (String hOffset) 
    { 
     this.hOffset = hOffset; 
    } 

    public String getHeight() 
    { 
     return height; 
    } 

    public void setHeight (String height) 
    { 
     this.height = height; 
    } 

    public String getWidth() 
    { 
     return width; 
    } 

    public void setWidth (String width) 
    { 
     this.width = width; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public void setName (String name) 
    { 
     this.name = name; 
    } 

    public String getSrc() 
    { 
     return src; 
    } 

    public void setSrc (String src) 
    { 
     this.src = src; 
    } 

    public String getVOffset() 
    { 
     return vOffset; 
    } 

    public void setVOffset (String vOffset) 
    { 
     this.vOffset = vOffset; 
    } 

    @Override 
    public String toString() 
    { 
     return "ClassPojo [alignment = "+alignment+", title = "+title+", hOffset = "+hOffset+", height = "+height+", width = "+width+", name = "+name+", src = "+src+", vOffset = "+vOffset+"]"; 
    } 
} 
2

改造只能處理一種數據類型。也許你可以使用authenticated_user & unauthenticated_user對象來創建主用戶對象。也許你應該要求後端團隊返回一個布爾值或任何變量來標識數據類型。

class User { 
AuthUser authUser; 
UnAuthUser unAuthUSer; 
}