最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談RxJava+Retrofit+OkHttp 封裝使用

 更新時(shí)間:2017年11月17日 09:35:18   作者:wzgiceman  
本篇文章主要介紹了淺談RxJava+Retrofit+OkHttp 懶人方式使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

背景

之前學(xué)習(xí)完Retrofit+Rxjava之后寫了一篇關(guān)于封裝的博客,發(fā)出后受到大家的關(guān)注以及使用,由于不斷的完善之前的項(xiàng)目,所以決定把最新的項(xiàng)目封裝過程講解出來,供大家查看!

Retrofit介紹:

Retrofit和okHttp師出同門,也是Square的開源庫(kù),它是一個(gè)類型安全的網(wǎng)絡(luò)請(qǐng)求庫(kù),Retrofit簡(jiǎn)化了網(wǎng)絡(luò)請(qǐng)求流程,基于OkHtttp做了封裝,解耦的更徹底:比方說通過注解來配置請(qǐng)求參數(shù),通過工廠來生成CallAdapter,Converter,你可以使用不同的請(qǐng)求適配器(CallAdapter), 比方說RxJava,Java8, Guava。你可以使用不同的反序列化工具(Converter),比方說json, protobuff, xml, moshi等等。

官網(wǎng) http://square.github.io/retrofit/

github https://github.com/square/retrofit

效果

懶人簡(jiǎn)單的使用方式

為什么稱為懶人,因?yàn)槟闶裁炊疾挥米?,直接按照一般案例寫rx和retrofit的使用

引入需要的包

  /*rx-android-java*/
  compile 'io.reactivex:rxjava:+'
  compile 'com.squareup.retrofit:adapter-rxjava:+'
  compile 'com.trello:rxlifecycle:+'
  compile 'com.trello:rxlifecycle-components:+'
  /*rotrofit*/
  compile 'com.squareup.retrofit2:retrofit:+'
  compile 'com.squareup.retrofit2:converter-gson:+'
  compile 'com.squareup.retrofit2:adapter-rxjava:+'
  compile 'com.google.code.gson:gson:+'

創(chuàng)建一個(gè)service定義請(qǐng)求的接口

/**
 * service統(tǒng)一接口數(shù)據(jù)
 * Created by WZG on 2016/7/16.
 */
public interface HttpService {
  @POST("AppFiftyToneGraph/videoLink")
  Observable<RetrofitEntity> getAllVedioBy(@Body boolean once_no);
}

創(chuàng)建一個(gè)retrofit對(duì)象

 //手動(dòng)創(chuàng)建一個(gè)OkHttpClient并設(shè)置超時(shí)時(shí)間
    okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(5, TimeUnit.SECONDS);

    Retrofit retrofit = new Retrofit.Builder()
        .client(builder.build())
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .baseUrl(HttpManager.BASE_URL)
        .build();

http請(qǐng)求處理

//    加載框
    final ProgressDialog pd = new ProgressDialog(this);

    HttpService apiService = retrofit.create(HttpService.class);
    Observable<RetrofitEntity> observable = apiService.getAllVedioBy(true);
    observable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            new Subscriber<RetrofitEntity>() {
              @Override
              public void onCompleted() {
                if (pd != null && pd.isShowing()) {
                  pd.dismiss();
                }
              }

              @Override
              public void onError(Throwable e) {
                if (pd != null && pd.isShowing()) {
                  pd.dismiss();
                }
              }

              @Override
              public void onNext(RetrofitEntity retrofitEntity) {
                tvMsg.setText("無封裝:\n" + retrofitEntity.getData().toString());
              }

              @Override
              public void onStart() {
                super.onStart();
                pd.show();
              }
            }

        );

源碼:傳送門-源碼地址

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

桓台县| 资讯 | 石景山区| 巨鹿县| 台中市| 麻栗坡县| 玉门市| 忻州市| 莱芜市| 客服| 扎赉特旗| 普定县| 凉山| 连城县| 于都县| 隆昌县| 嵊州市| 静宁县| 康平县| 星座| 衡阳县| 武陟县| 阳谷县| 秀山| 芮城县| 双辽市| 双流县| 柳林县| 金门县| 县级市| 镇赉县| 宝丰县| 黑河市| 文昌市| 新丰县| 西充县| 沁源县| 慈利县| 改则县| 白玉县| 弋阳县|