這篇文章給大家分享的是有關(guān)RxJava+Retrofit+OkHttp封裝的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號搭建、微信平臺小程序開發(fā)、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
Retrofit介紹:
Retrofit和okHttp師出同門,也是Square的開源庫,它是一個(gè)類型安全的網(wǎng)絡(luò)請求庫,Retrofit簡化了網(wǎng)絡(luò)請求流程,基于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
效果

懶人簡單的使用方式
為什么稱為懶人,因?yàn)槟闶裁炊疾挥米觯苯影凑找话惆咐龑憆x和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定義請求的接口
/**
 * 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對象
//手動創(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請求處理
//    加載框
    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();
              }
            }
        );感謝各位的閱讀!關(guān)于“RxJava+Retrofit+OkHttp封裝的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
                分享文章:RxJava+Retrofit+OkHttp封裝的示例分析
                
                當(dāng)前路徑:http://www.chinadenli.net/article16/ishgdg.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、自適應(yīng)網(wǎng)站、虛擬主機(jī)、微信小程序、網(wǎng)站排名、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
