源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

浅谈RxJava+Retrofit+OkHttp 封装使用

  • 时间:2022-05-19 20:34 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:浅谈RxJava+Retrofit+OkHttp 封装使用
[b]背景[/b] 之前学习完Retrofit+Rxjava之后写了一篇关于封装的博客,发出后受到大家的关注以及使用,由于不断的完善之前的项目,所以决定把最新的项目封装过程讲解出来,供大家查看! [b]Retrofit介绍:[/b] Retrofit和okHttp师出同门,也是Square的开源库,它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底:比方说通过注解来配置请求参数,通过工厂来生成CallAdapter,Converter,你可以使用不同的请求适配器(CallAdapter), 比方说RxJava,Java8, Guava。你可以使用不同的反序列化工具(Converter),比方说json, protobuff, xml, moshi等等。 官网 [url=http://square.github.io/retrofit/]http://square.github.io/retrofit/[/url] github [url=https://github.com/square/retrofit]https://github.com/square/retrofit[/url] 效果 [img]http://files.jb51.net/file_images/article/201711/201711170934433.gif?201710179354[/img] [b]懒人简单的使用方式[/b] 为什么称为懒人,因为你什么都不用做,直接按照一般案例写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:+'
创建一个service定义请求的接口
/**
 * service统一接口数据
 * Created by WZG on 2016/7/16.
 */
public interface HttpService {
  @POST("AppFiftyToneGraph/videoLink")
  Observable<RetrofitEntity> getAllVedioBy(@Body boolean once_no);
}
创建一个retrofit对象
 //手动创建一个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();
              }
            }

        );
源码:[url=http://xiazai.jb51.net/201711/yuanma/RxjavaRetrofitDemo_jb51.rar]传送门-源码地址[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部