2016-08-31 3 views
1

こんにちは私は私のコールバックがRetrofit Errorコールバックでカスタムオブジェクトを追加します。 ONFAILURE

 @Override 
public void onResponse(final Call<T> call, Response<T> response) { 

    if (response.isSuccessful()) { 
     passing this to my view 

    } else { 


//  as this failed other then 200  retroCallback.onFailure(call, new Throwable("")); 

    } 
} 

@Override 
public void onFailure(Call<T> call, Throwable t) { 
    retroCallback.onFailure(call, t); 
} 

に従うように、この中で、私はとにかく代わりThrowableの私のErrorBeanを渡すことができますどのように我々はONFAILUREにカスタムモデルを渡すことができている改造使用していますか?私のサーバーとして私の応答を与えるいくつかのformate私はその形式を渡すしたいと思います。私はretrofit 2.1.0を使用しています

+0

http://stackoverflow.com/questions/35029936/centralized-error-handling-retrofit-2が役立ちます。 – Amir

答えて

1

あなたはThrowableをサブクラス化し、

public class ErrorBean extends Throwable { 
    public ErrorPayload payload = null; 

    public ErrorBean(ErrorPayload payload) { 
     this.payload = payload; 
    } 
} 

その後、のonErrorで:

@Override 
public void onFailure(Call<T> call, Throwable t) { 
    retroCallback.onFailure(call, t); 
    if (t instanceof ErrorBean) { 
     // do your stuff here 
     ((ErrorBean)t).payload.text; 
    } 
} 
0

私の知る限り,,レトロフィットのONFAILUREはありません、インターネット接続などのエラーを処理するために使用されます。

サーバーからのエラー応答、エラー応答を処理するには、4xxステータスコードを持つサーバーからの応答を意味しますが、それを処理するクライアント用のJSON応答があります。

言って、あなたがサーバーからこのエラー構造を得ている:

{ 
    statusCode: 409, 
    message: "Email address already registered" 
} 

このエラーはonResponse(...)にキャプチャされます。これに対処するには、作成あなたの

public class ErrorBean { 

    private int statusCode; 
    private String message; 

    public ErrorBean() { 
    } 

    public int status() { 
     return statusCode; 
    } 

    public String message() { 
     return message; 
    } 
} 

簡単なのErrorHandlerのUTILを作成します。

public class ErrorUtils { 

    public static ErrorBean parseError(Response<?> response) { 
     Converter<ResponseBody, ErrorBean> converter = 
       ServiceGenerator.retrofit() 
         .responseBodyConverter(ErrorBean.class, new Annotation[0]); 

     ErrorBean error; 

     try { 
      error = converter.convert(response.errorBody()); 
     } catch (IOException e) { 
      return new ErrorBean(); 
     } 

     return error; 
    } 
} 

そして最後に、

... 
call.enqueue(new Callback<SuccessResponse>() { 
    @Override 
    public void onResponse(Call<SuccessResponse> call, Response<SuccessResponse> response) { 
     if (response.isSuccessful()) { 
      // use response data and do some fancy stuff :) 
     } else { 
      // parse the response body … 
      ErrorBean error = ErrorUtils.parseError(response); 
      // … and use it to show error information 

      // … or just log the issue like we’re doing :) 
      Log.d("error message", error.message()); 
     } 
    } 

    @Override 
    public void onFailure(Call<User> call, Throwable t) { 
     // there is more than just a failing request (like: no internet connection) 
    } 
}); 

は、あなたがポイントを得たホープ.. !!!

+0

だから私はコールバックから基本的に私はその共通のクラスですべての計算を行いたいと思って渡してから拡張している1つの共通のクラスを行っている。だからあなたのやり方で私は(response.isSuccessful()){}すべての私のAPI呼び出しと – andro

関連する問題