RxJava + Retrofit + okhttp 的實際開發(fā)應用

注:此文章不談其他用法涛目,談實際開發(fā)中最普遍的用法

分析

實際需求:
返回json格式:

{
"code": 1,
"msg": "",
"time": 1470717690,
"data": T
}

其中data千變?nèi)f化怠惶,但是總體來說是code決定了data的內(nèi)容扰才,所以在解析response返回值時需要對code進行一系列邏輯處理,這很重要凌停。

以一個最常用的使用場景為例侮东,去服務器取得我的新聞列表圈盔,因為我的這個其中涉及了權(quán)限驗證即token的獲取,當然也包含了token狀態(tài)的過期以及服務器異常導致token無法通過驗證的場景悄雅,基本所有情況都考慮到了驱敲。

下面我以代碼的順序為主來說明,防止扯來扯去宽闲,扯的自己都不知道說道哪里了众眨。

前提:最好了解一點相關知識,關于三者Rxjava容诬、Retrofit娩梨、Okhttp

gradle 引入的一些類庫(三者Rxjava、Retrofit览徒、Okhttp)

    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.7'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.orhanobut:logger:1.15'

構(gòu)建工具類

我的主包:com.rz.app

public class HttpMethods {
    private static OkHttpClient okHttpClient = new OkHttpClient();
    private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
    private static Converter.Factory advancedGsonConverterFactory = com.rz.app.api.convert.GsonConverterFactory.create();
    private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();
    public static Retrofit getApi() {
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("http://api.com/")
                .addConverterFactory(advancedGsonConverterFactory)
                .addCallAdapterFactory(rxJavaCallAdapterFactory)
                .build();
        return retrofit;
    }
}

重寫gsoncovertfactory相關類(一共三個)

因為官方自帶的這個處理轉(zhuǎn)化類實在是粗糙狈定,如果data的格式不對,那么會直接拋出JsonSyntaxException习蓬,這就很暴力了纽什,我們應該根據(jù)code來判斷相應的邏輯。舉個例子:想獲取新聞列表友雳,那么data是數(shù)組格式稿湿,但是如果token過期,被中間件攔截(可以理解成權(quán)限驗證)押赊,這個data就不返回或者干脆返回空值饺藤,那么會拋出這個異常,無法獲取code不能準確做出相應的邏輯處理流礁。

先和服務端約定好錯誤碼

  • 我定義此異常
//code 為-9
public class ErrorException extends RuntimeException{
    public ErrorException(String s) {
        super(s);
    }
}
//code 為-1
public class GetTokenException extends RuntimeException {
}
//code 為0
public class MsgException extends RuntimeException{
    public MsgException(String s) {
        super(s);
    }
}
//code 為-2
public class NeedLoginException extends RuntimeException{
    public NeedLoginException(String s) {
        super(s);
    }
}

解釋一下:
-9 請求錯誤(api的url錯誤)涕俗、服務端異常
-2 賬號有誤,客戶端記錄的賬號不正確,會跳到登入頁面
-1 token過期
0 正常情況下錯誤狀態(tài)

  • 定義code的result類,用來作為中間返回值類
public class Result {
    private int code;
    private String msg;
    private int time;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

}
  • 定義返回值類高帖,注意和上面的區(qū)別
public class Msg<T> {
    private int code;
    private String msg;
    private int time;
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
  • copy源碼,首先copy過來三個文件元镀,其實主要是修改GsonResponseBodyConverter绍填。
    @Override
    public T convert(ResponseBody value) throws IOException {
        String respose = value.string();
        Result msg = gson.fromJson(respose, Result.class); // Result即為上面定義的中間返回值類

        if (msg.getCode() < 1) {
            value.close();
            switch (msg.getCode()) {
                case -9:
                    throw new ErrorException(msg.getMsg());

                case -2:
                    throw new NeedLoginException("需要登入");

                case -1:
                    Logger.d("token 過期");
                    throw new GetTokenException();

                case 0:
                    throw new MsgException(msg.getMsg());
            }
            throw new ErrorException("未定義錯誤碼");
        }

        MediaType mediaType = value.contentType();
        Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
        InputStream inputStream = new ByteArrayInputStream(respose.getBytes());
        Reader reader = new InputStreamReader(inputStream,charset);

        JsonReader jsonReader = gson.newJsonReader(reader);

        try {
            return adapter.read(jsonReader);
        } finally {
            value.close();
        }
    }

注意這里有個坑,使用不當會拋出java.lang.IllegalStateException栖疑,可以參考這位小伙伴http://www.reibang.com/p/5b8b1062866b

因為你只能對ResponseBody讀取一次 , 如果你調(diào)用了response.body().string()兩次或者response.body().charStream()兩次就會出現(xiàn)這個異常, 先調(diào)用string()再調(diào)用charStream()也不可以讨永。
所以通常的做法是讀取一次之后就保存起來,下次就不從ResponseBody里讀取。

Rxjava主題邏輯

好了做了這么多的準備工作重要遇革,開始重頭戲了卿闹,先說一下思路,

判斷token:
----1為null:拋出GetTokenException萝快,在retryWhen中用存儲的賬號網(wǎng)絡請求token
----2不為空:flatMap中請求news數(shù)據(jù)

1中:
----1.1賬號錯誤锻霎,拋出NeedLoginException,那么直接跳到登入頁面
----1.2獲得了token揪漩,存儲起來旋恼,并且flatMap中請求news數(shù)據(jù)

1.2中:
----1.2.1如果服務器出現(xiàn)異常,即無法驗證token氢拥,返回-1蚌铜,拋出GetTokenException,會重復獲取token如果不加處理嫩海,那就gg了,所以加個zipWith囚痴,只請求三次叁怪,第四次直接拋出ErrorException("服務器異常"),遠離gg

2中
----返回-1深滚,token過期奕谭,那么同1.2.1

retryWhen為中間處理層,subscribe中onError終極處理

假設token我們直接定義為

public class Token extends BaseModel {
    private String actoken;
    private int time;

    public String getActoken() {
        return actoken;
    }

    public void setActoken(String actoken) {
        this.actoken = actoken;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

}

接口如下定義痴荐,News為新聞類(就不貼了)

public class ApiService {
    public interface GetTokenApi {
        @GET("index/gettoken/mobile/{mobile}/password/{password}")
        Observable<Msg<Token>> result(@Path("mobile") String mobile,@Path("password") String password);
    }

    public interface NewsApi {
        @GET("mycenter/getNews/token/{token}")
        Observable<Msg<List<News>>> result(@Path("token") String token);
    }
}

下面開始Rxjava處理主要邏輯

    protected Token token=null;
    protected Subscription subscription;
    //解除訂閱
    protected void unsubscribe() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }

    ...

    public void getNews(){
        unsubscribe();
        subscription = Observable.just(null)
            .flatMap(new Func1<Object,Observable<List<News>>>(){

                @Override
                public Observable<List<News>> call(Object o) {
                    if(token == null){
                        Logger.d("token為null");
                        return Observable.error(new GetTokenException());
                    }
                    Logger.d("使用緩存的token");
                    // TODO: 本地判斷token是否過期血柳,當然服務器也會二次判斷
          
                    return getApi().create(ApiService.NewsApi.class)
                            .result(token.getActoken())
                            .map(new Func1<Msg<List<News>>, List<News>>() {
                                @Override
                                public List<News> call(Msg<List<News>> listMsg) {
                                    return listMsg.getData();
                                }
                            });
                }
            })
            .retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
                @Override
                public Observable<?> call(Observable<? extends Throwable> observable) {
                    return observable
                        .zipWith(Observable.range(1, 4), new Func2<Throwable, Integer, Throwable>() {
                            @Override
                            public Throwable call(Throwable throwable, Integer integer) {
                                if(integer == 4){
                                    throw new ErrorException("服務器異常");
                                }
                                return throwable;
                            }
                        })
                        .flatMap(new Func1<Throwable, Observable<?>>() {
                            @Override
                            public Observable<?> call(final Throwable throwable) {

                                if (throwable instanceof GetTokenException) {
                                    //TODO 獲取存儲的賬戶信息,用來獲取token
                                    //如果沒有生兆,即首次登入难捌,或者token過期,或者剛剛客戶端注銷等業(yè)務判斷  需要拋出一個NeedLoginExcption
                                    // 這里假設有記錄
                                    boolean firstLogin = false;
                                    boolean tokenExpired = false;
                                    boolean logoff = false;
                                    if(firstLogin || tokenExpired || logoff){
                                        return Observable.error(new NeedLoginException("需要登入"));
                                    }
                                    return getApi()
                                        .create(ApiService.GetTokenApi.class)
                                        .result("12345678910", "123456")
                                        .map(new Func1<Msg<Token>, Token>() {
                                            @Override
                                            public Token call(Msg<Token> msg) {
                                                return msg.getData();
                                            }
                                        })
                                        .doOnNext(new Action1<Token>() {
                                            @Override
                                            public void call(Token t) {
                                                Logger.d("存儲token");
                                                //TODO: 存入緩存等
                                                token = t;
                                            }
                                        });
                                }
                                
                                return Observable.error(throwable);
                            }
                        });
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())

            .subscribe(new Subscriber<List<News>>() {
                @Override
                public void onCompleted() {
                    //TODO: 完成邏輯
                }

                @Override
                public void onError(Throwable throwable) {
                    if(throwable instanceof NeedLoginException){
                        //TODO: 跳到登入頁面
                    }else if(throwable instanceof ErrorException){
                        //TODO: 提示 throwable.getMessage()
                    }else if(throwable instanceof MsgException) {
                        //TODO: 提示 throwable.getMessage()
                    }else {
                        Logger.d(throwable.getClass());
                        //TODO: 還剩下網(wǎng)絡異常處理
                    }
                }

                @Override
                public void onNext(List<KrNews> krNewses) {
                    //TODO: 更新ui
                }
            });
    }

附上我測試的結(jié)果圖

token為空鸦难,會先使用賬號獲取token根吁,在獲取目標數(shù)據(jù)

1.gif

token可用,直接使用緩存的token

2.gif

服務器錯誤合蔽,嘗試3次击敌,第四次拋出異常,途中循環(huán)了三次

3.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拴事,一起剝皮案震驚了整個濱河市沃斤,隨后出現(xiàn)的幾起案子圣蝎,更是在濱河造成了極大的恐慌,老刑警劉巖衡瓶,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捅彻,死亡現(xiàn)場離奇詭異,居然都是意外死亡鞍陨,警方通過查閱死者的電腦和手機步淹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诚撵,“玉大人缭裆,你說我怎么就攤上這事∈傺蹋” “怎么了澈驼?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長筛武。 經(jīng)常有香客問我缝其,道長,這世上最難降的妖魔是什么徘六? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任内边,我火速辦了婚禮,結(jié)果婚禮上待锈,老公的妹妹穿的比我還像新娘漠其。我一直安慰自己,他們只是感情好竿音,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布和屎。 她就那樣靜靜地躺著,像睡著了一般春瞬。 火紅的嫁衣襯著肌膚如雪柴信。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天宽气,我揣著相機與錄音随常,去河邊找鬼。 笑死抹竹,一個胖子當著我的面吹牛线罕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播窃判,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼钞楼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了袄琳?” 一聲冷哼從身側(cè)響起询件,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤燃乍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后宛琅,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刻蟹,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年嘿辟,在試婚紗的時候發(fā)現(xiàn)自己被綠了舆瘪。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡红伦,死狀恐怖英古,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情昙读,我是刑警寧澤召调,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站蛮浑,受9級特大地震影響唠叛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜沮稚,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一艺沼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧壮虫,春花似錦澳厢、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽线得。三九已至饶唤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贯钩,已是汗流浹背募狂。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留角雷,地道東北人祸穷。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像勺三,于是被迫代替她去往敵國和親雷滚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容