【Android架構】基於MVP模式的Retrofit2+RXjava封裝之資料預處理(六)

歡子發表於2019-03-06

前言

mvp框架也用了相當長的時間了,一般讓人比較糾結的就是後臺資料的處理問題。大多數的公司由於程式碼的不規範、經手人員太多等等原因,後臺的程式碼相當混亂,介面返回的資料格式也五花八門,當然,如果你能直接讓後臺大哥改程式碼的話,就另當別論,大多數情況還是要Android端來背鍋。這裡,我們就聊聊這個。

一般套路

我們會直接複製介面返回的json,然後用外掛轉換為實體類(國際慣例,不貼get和set)

public class ShareModel {
 
    private int status;
    private String msg;
    private List<DataBean> data;
    public static class DataBean {
        private String id;
        private String wshare_name;
        private String wshare_head;
        private String wshare_content;
  }
}
複製程式碼

進階套路

後臺返回的資料格式如下:

{
	"status": 1,
	"msg": "請求成功",
	"data": []
}
複製程式碼

我們會定義一個BaseModel(國際慣例,不貼get和set)

public class BaseModel<T> implements Serializable {
    private int status;
    private String msg;
    private T data;
}
複製程式碼

如果datalist的話,還會定義個BaseListModel,只是其中的dataList<T>而已。 然後,在ApiServer中定義介面

 
    @FormUrlEncoded
    @POST("/mapi/index.php?ctl=user&act=userbaseinfo")
    Observable<BaseModel<UserModel>> getUserInfo(@FieldMap Map<String, String> params);
複製程式碼

presenter中使用

    /**
     * 獲取使用者詳情
     *
     * @param params
     */
    public void getUserInfo(Map<String, String> params) {
        addDisposable(apiServer.getUserInfo(params), new BaseObserver<BaseModel<UserModel>>(baseView) {

            @Override
            public void onSuccess(BaseModel<UserModel> o) {
                baseView.onGetUserInfoSucc(o.getData());

            }

            @Override
            public void onError(String msg) {
                baseView.showError(msg);
            }
        });

    }
複製程式碼

然後回撥到activity或者fragment中處理,這部分就不詳細說了,可以看看之前的文章。

這樣看似沒有問題,但是如果後臺某個介面返回的資料的格式如下,

{
	"status": 1,
	"error": "請求成功",
	"data": []
}
複製程式碼

有人說了,在BaseModelBaseListModel再加一個error欄位不就好了?

如果資料是這樣呢?

{
	"code": 1,
	"error": "請求成功",
	"data": []
}
複製程式碼

可能這張圖能表達你現在的心情

image.png

終極套路

雖然生活如此艱難,但是問題還是要解決的。

我們可以回想一下,http請求返回的是物件是ResponseBody,它是怎麼轉換為我們的實體類呢? 主要程式碼在這裡 retrofit.addConverterFactory(GsonConverterFactory.create())

我們跟進去看看

public final class GsonConverterFactory extends Converter.Factory {
  /**
   * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  public static GsonConverterFactory create() {
    return create(new Gson());
  }

  /**
   * Create an instance using {@code gson} for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  @SuppressWarnings("ConstantConditions") // Guarding public API nullability.
  public static GsonConverterFactory create(Gson gson) {
    if (gson == null) throw new NullPointerException("gson == null");
    return new GsonConverterFactory(gson);
  }

  private final Gson gson;

  private GsonConverterFactory(Gson gson) {
    this.gson = gson;
  }

  @Override
  public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
      Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonResponseBodyConverter<>(gson, adapter);
  }

  @Override
  public Converter<?, RequestBody> requestBodyConverter(Type type,
      Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonRequestBodyConverter<>(gson, adapter);
  }
}
複製程式碼

可以看到,主要邏輯是在GsonResponseBodyConverter裡面

final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
  private final Gson gson;
  private final TypeAdapter<T> adapter;

  GsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
    this.gson = gson;
    this.adapter = adapter;
  }

  @Override public T convert(ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    try {
      T result = adapter.read(jsonReader);
      if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
        throw new JsonIOException("JSON document was not fully consumed.");
      }
      return result;
    } finally {
      value.close();
    }
  }
}
複製程式碼

可以看到,先是拿到位元組流,然後呼叫TypeAdapterread方法,轉換為我們的實體類,這個原理我們先不深究,後面有時間在講。這裡我們能不能做文章呢,答案是可以。

首先,這幾個類都是final修飾的,不能被繼承,不過沒事,我們可以複製這幾個類的程式碼,然後改個名字

image.png

其中,BaseConverterFactoryBaseRequestBodyConverter與原始碼一致,只需要修改類名即可。重點在BaseResponseBodyConverter

public class BaseResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    BaseResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {

        String jsonString = value.string();
        try {
            JSONObject object = new JSONObject(jsonString);

            int code = object.getInt("code");
            if (code != 1) {
                String msg = object.getString("msg");
                if (TextUtils.isEmpty(msg)) {
                    msg = object.getString("error");
                }
                //異常處理
                throw new BaseException(msg, code);
            }

            return adapter.fromJson(object.getString("data"));

        } catch (JSONException e) {
            e.printStackTrace();
            //資料解析異常
            throw new BaseException(BaseException.PARSE_ERROR_MSG, BaseException.PARSE_ERROR);
        } finally {
            value.close();
        }
    }
}
複製程式碼

判斷的程式碼可以自己根據專案需要,自行新增 BaseException

public class BaseException extends IOException {

    /**
     * 解析資料失敗
     */
    public static final int PARSE_ERROR = 1001;
    public static final String PARSE_ERROR_MSG = "解析資料失敗";

    /**
     * 網路問題
     */
    public static final int BAD_NETWORK = 1002;
    public static final String BAD_NETWORK_MSG = "網路問題";
    /**
     * 連線錯誤
     */
    public static final int CONNECT_ERROR = 1003;
    public static final String CONNECT_ERROR_MSG = "連線錯誤";
    /**
     * 連線超時
     */
    public static final int CONNECT_TIMEOUT = 1004;
    public static final String CONNECT_TIMEOUT_MSG = "連線超時";
    /**
     * 未知錯誤
     */
    public static final int OTHER = 1005;
    public static final String OTHER_MSG = "未知錯誤";


    private String errorMsg;
    private int errorCode;


    public String getErrorMsg() {
        return errorMsg;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public BaseException(String errorMsg, Throwable cause) {
        super(errorMsg, cause);
        this.errorMsg = errorMsg;
    }

    public BaseException(String message, Throwable cause, int errorCode) {
        super(message, cause);
        this.errorCode = errorCode;
        this.errorMsg = message;
    }

    public BaseException(String message, int errorCode) {
        this.errorCode = errorCode;
        this.errorMsg = message;
    }
}
複製程式碼

修改BaseObserver程式碼,onNext中只處理成功回撥,onError中處理各種異常

public abstract class BaseObserver<T> extends DisposableObserver<T> {

    protected BaseView view;

    private boolean isShowDialog;


    public BaseObserver(BaseView view) {
        this.view = view;
    }

    public BaseObserver(BaseView view, boolean isShowDialog) {
        this.view = view;
        this.isShowDialog = isShowDialog;
    }

    @Override
    protected void onStart() {
        if (view != null && isShowDialog) {
            view.showLoading();
        }
    }

    @Override
    public void onNext(T o) {
        onSuccess(o);
    }

    @Override
    public void onError(Throwable e) {
        if (view != null && isShowDialog) {
            view.hideLoading();
        }
        BaseException be = null;

        if (e != null) {

            if (e instanceof BaseException) {
                be = (BaseException) e;

                //回撥到view層 處理 或者根據專案情況處理
                if (view != null) {
                    view.onErrorCode(new BaseModel(be.getErrorCode(), be.getErrorMsg()));
                } else {
                    onError(be.getErrorMsg());
                }

            } else {
                if (e instanceof HttpException) {
                    //   HTTP錯誤
                    be = new BaseException(BaseException.BAD_NETWORK_MSG, e, BaseException.BAD_NETWORK);
                } else if (e instanceof ConnectException
                        || e instanceof UnknownHostException) {
                    //   連線錯誤
                    be = new BaseException(BaseException.CONNECT_ERROR_MSG, e, BaseException.CONNECT_ERROR);
                } else if (e instanceof InterruptedIOException) {
                    //  連線超時
                    be = new BaseException(BaseException.CONNECT_TIMEOUT_MSG, e, BaseException.CONNECT_TIMEOUT);
                } else if (e instanceof JsonParseException
                        || e instanceof JSONException
                        || e instanceof ParseException) {
                    //  解析錯誤
                    be = new BaseException(BaseException.PARSE_ERROR_MSG, e, BaseException.PARSE_ERROR);
                } else {
                    be = new BaseException(BaseException.OTHER_MSG, e, BaseException.OTHER);
                }
            }
        } else {
            be = new BaseException(BaseException.OTHER_MSG, e, BaseException.OTHER);
        }

        onError(be.getErrorMsg());

    }

    @Override
    public void onComplete() {
        if (view != null && isShowDialog) {
            view.hideLoading();
        }

    }


    public abstract void onSuccess(T o);

    public abstract void onError(String msg);


}

複製程式碼

ApiRetrofit中新增我們自定義的ConverterFactory

 .addConverterFactory(BaseConverterFactory.create())
複製程式碼

這樣的話,ApiServer便可以這樣定義了

 @FormUrlEncoded
    @POST("/mapi/index.php?ctl=user&act=userbaseinfo")
    Observable<UserModel> getUserInfo(@FieldMap Map<String, String> params);
複製程式碼

相應的,presenter可以這樣寫

  public void getUserInfo(Map<String, String> params) {
        addDisposable(apiServer.getUserInfo(params), new BaseObserver<UserModel>(baseView) {

            @Override
            public void onSuccess(UserModel o) {
                baseView.onGetUserInfoSucc(o);

            }

            @Override
            public void onError(String msg) {
                baseView.showError(msg);
            }
        });

    }
複製程式碼

是不是精簡了許多,快來試試吧

參考 RxJava2 + Retrofit2 完全指南 之 統一狀態碼/Exception處理

最後,獻上原始碼 Github

你的認可,是我堅持更新部落格的動力,如果覺得有用,就請點個贊,謝謝

相關文章