Giter VIP home page Giter VIP logo

retrofit-rxjava-okhttp's Introduction

retrofit-rxjava-okhttp

Retrofit-RxJava-OkHttp二次封装
正在逐渐完善,有问题和建议也可以在issues提出
###Screenshot Example1

###Android Studio - 在build.gradle中引入

compile 'com.dyhdyh.rro:retrofit-rxjava-okhttp:1.0.0-Preview'

###需要的依赖

compile 'com.squareup.okhttp3:okhttp:3.5.0'
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.squareup.retrofit2:converter-scalars:2.1.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

####Example 初始化配置

//初始化配置
public class DemoApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();

        //默认配置(15秒超时,HttpLoggingInterceptor,ScalarsConverterFactory,GsonConverterFactory,RxJavaCallAdapterFactory)
        //传入接口BaseUrl,是否输出log
        RxRetrofitClient.init(BuildConfig.API_URL,BuildConfig.DEBUG);

        //自定义配置
        /*
        RxRetrofitConfig config = new RxRetrofitConfig.Builder().connectTimeout(20 * 1000)
                //.addInterceptor()
                //.addConverterFactory()
                //.addCallAdapterFactory()
                .build();
        RxRetrofitClient.init(config);
        */
    }
}

请求的两种用法 ```java /** * lambda写法 */ private void requestByLambda() { RxRetrofitClient.create(IMovieApi.class).getHotMovie() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnSubscribe(() -> { Log.d(TAG, Thread.currentThread().getName() + "---onStart......"); //显示loading LoadingBar.show(rlContent); }) .subscribe(hotMovieModel -> { Log.d(TAG, Thread.currentThread().getName() + "---onNext......" + hotMovieModel); //绑定数据 bindData(hotMovieModel.getSubjects()); }, throwable -> { Log.d(TAG, Thread.currentThread().getName() + "---onError......" + throwable); //取消loading LoadingBar.cancel(rlContent); }, () -> { Log.d(TAG, Thread.currentThread().getName() + "---onCompleted......"); //取消loading LoadingBar.cancel(rlContent); }); }
/**
 * subscribe写法
 */
public void requestBySubscribe() {
    RxRetrofitClient.create(IMovieApi.class).getHotMovie()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<HotMovieModel>() {
                @Override
                public void onStart() {
                    Log.d(TAG, Thread.currentThread().getName() + "---onStart......");
                    //显示loading
                    LoadingBar.show(rlContent);
                }

                @Override
                public void onCompleted() {
                    Log.d(TAG, Thread.currentThread().getName() + "---onCompleted......");
                    //取消loading
                    LoadingBar.cancel(rlContent);
                }

                @Override
                public void onError(Throwable e) {
                    Log.d(TAG, Thread.currentThread().getName() + "---onError......" + e);
                    //取消loading
                    LoadingBar.cancel(rlContent);
                }

                @Override
                public void onNext(HotMovieModel hotMovieModel) {
                    Log.d(TAG, Thread.currentThread().getName() + "---onNext......" + hotMovieModel);
                    //绑定数据
                    bindData(hotMovieModel.getSubjects());
                }
            });
}


######Android交流QQ群:146262062
######http://dyhdyh.com/

retrofit-rxjava-okhttp's People

Contributors

xiandanin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.