RxJava+Retrofit+OkHttp封裝的示例分析

這篇文章給大家分享的是有關RxJava+Retrofit+OkHttp封裝的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設服務有做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)絡營銷策劃、網(wǎng)頁設計、網(wǎng)站維護、公眾號搭建、微信平臺小程序開發(fā)、軟件開發(fā)等為企業(yè)提供一整套的信息化建設解決方案。創(chuàng)造真正意義上的網(wǎng)站建設,為互聯(lián)網(wǎng)品牌在互動行銷領域創(chuàng)造價值而不懈努力!

Retrofit介紹:

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

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

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

效果

RxJava+Retrofit+OkHttp封裝的示例分析

懶人簡單的使用方式

為什么稱為懶人,因為你什么都不用做,直接按照一般案例寫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)建一個service定義請求的接口

/**
 * 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)建一個retrofit對象

 //手動創(chuàng)建一個OkHttpClient并設置超時時間
    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請求處理

//    加載框
    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();
              }
            }

        );

感謝各位的閱讀!關于“RxJava+Retrofit+OkHttp封裝的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

分享文章:RxJava+Retrofit+OkHttp封裝的示例分析
當前路徑:http://www.muchs.cn/article16/ishgdg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、自適應網(wǎng)站、虛擬主機微信小程序、網(wǎng)站排名、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化