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

前言

mvp框架也用了至關長的時間了,通常讓人比較糾結的就是後臺數據的處理問題。大多數的公司因爲代碼的不規範、經手人員太多等等緣由,後臺的代碼至關混亂,接口返回的數據格式也五花八門,固然,若是你能直接讓後臺大哥改代碼的話,就另當別論,大多數狀況仍是要Android端來背鍋。這裏,咱們就聊聊這個。php

通常套路

咱們會直接複製接口返回的json,而後用插件轉換爲實體類(國際慣例,不貼get和set)java

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;
  }
}
複製代碼

進階套路

後臺返回的數據格式以下:git

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

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

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

若是datalist的話,還會定義個BaseListModel,只是其中的dataList<T>而已。 而後,在ApiServer中定義接口json

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

presenter中使用api

/** * 獲取用戶詳情 * * @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字段不就行了?框架

若是數據是這樣呢?ide

{
	"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

你的承認,是我堅持更新博客的動力,若是以爲有用,就請點個贊,謝謝

相關文章
相關標籤/搜索