史上最簡單rxjava Retyofit 組合

Apiservice 接口 以下操作

   @GET(ApiServiceVar.JIEKOU+"?type=3&page=1")
   Flowable<MyDataBean> getHomeAd();
/*   @Query("type") String type, @Query("page") int page*/

ApiServiceVar  類 主要聲明bace
//基本接口
public static final String API_URL="https://www.apiopen.top/";
//視頻 圖片
public static final String JIEKOU="satinApi";
HttpUtils  類 主要封裝了Retyofit
private static volatile HttpUtils instance;
private final Retrofit retrofit;

private HttpUtils(){

    //攔截器
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
    OkHttpClient build = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .build();

    retrofit = new Retrofit.Builder()
            .baseUrl(ApiServiceVar.API_URL)
            .client(build)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
}

public static HttpUtils getInstance() {

    if(instance == null){
        synchronized (HttpUtils.class){
            if(null == instance){
                instance = new HttpUtils();
            }
        }
    }

    return instance;
}

public ApiService getApiService(){
    return retrofit.create(ApiService.class);
}

// Persenter要這麼寫

public void acttview(){
    Flowable<MyDataBean> homeAd = HttpUtils.getInstance().getApiService().getHomeAd();
    homeAd.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DefaultSubscriber<MyDataBean>() {
                @Override
                public void onNext(MyDataBean myDataBean) {
                    //接口回調的方法
                getMainView().suosee(myDataBean);
                }

                @Override
                public void onError(Throwable t) {

                }

                @Override
                public void onComplete() {

                }
            });
}

//最後下面依賴
 //tablalyut
    implementation 'com.android.support:design:27.1.1'
//okhttp
    implementation 'com.squareup.okhttp3:okhttp:3.6.0'
//butterknife
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//xrecyclerview
    compile 'com.jcodecraeer:xrecyclerview:1.5.9'
//recyclerview
    compile 'com.android.support:recyclerview-v7:27.1.1'
//eventbus
    compile 'org.greenrobot:eventbus:3.1.1'
// Retrofit庫
    compile 'com.squareup.okhttp3:okhttp:3.1.2'
// Okhttp庫
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
//引入Gson支持
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
// 針對rxjava2.x(adapter-rxjava2的版本要 >= 2.2.0)
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
//rxjava
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

    compile 'com.facebook.fresco:fresco:0.12.0'

    // 支持 GIF 動圖,需要添加
    compile 'com.facebook.fresco:animated-gif:0.12.0'

    // 支持 WebP (靜態圖+動圖),需要添加
    compile 'com.facebook.fresco:animated-webp:0.12.0'
    compile 'com.facebook.fresco:webpsupport:0.12.0'

    // 僅支持 WebP 靜態圖,需要添加
    compile 'com.facebook.fresco:webpsupport:0.12.0'


    implementation 'com.google.code.gson:gson:2.2.4'
//底部導航
    compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0'
//頂部伸縮
    implementation 'com.android.support:design:27.1.1'
//xbanner
    compile 'com.xhb:xbanner:1.3.7'
//Glide
    compile 'com.github.bumptech.glide:glide:3.7.0'
//社區右上方加號依賴
    compile 'com.zaaach:toprightmenu:1.1.2'

    //picasso依賴
    compile 'com.squareup.picasso:picasso:2.5.2'

    compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
 //下面是配置
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章