淺談RxJava+Retrofit+OkHttp 封裝使用
背景
之前學(xué)習(xí)完Retrofit+Rxjava之后寫(xiě)了一篇關(guān)于封裝的博客,發(fā)出后受到大家的關(guān)注以及使用,由于不斷的完善之前的項(xiàng)目,所以決定把最新的項(xiàng)目封裝過(guò)程講解出來(lái),供大家查看!
Retrofit介紹:
Retrofit和okHttp師出同門(mén),也是Square的開(kāi)源庫(kù),它是一個(gè)類型安全的網(wǎng)絡(luò)請(qǐng)求庫(kù),Retrofit簡(jiǎn)化了網(wǎng)絡(luò)請(qǐng)求流程,基于OkHtttp做了封裝,解耦的更徹底:比方說(shuō)通過(guò)注解來(lái)配置請(qǐng)求參數(shù),通過(guò)工廠來(lái)生成CallAdapter,Converter,你可以使用不同的請(qǐng)求適配器(CallAdapter), 比方說(shuō)RxJava,Java8, Guava。你可以使用不同的反序列化工具(Converter),比方說(shuō)json, protobuff, xml, moshi等等。
官網(wǎng) http://square.github.io/retrofit/
github https://github.com/square/retrofit
效果

懶人簡(jiǎn)單的使用方式
為什么稱為懶人,因?yàn)槟闶裁炊疾挥米觯苯影凑找话惆咐龑?xiě)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)建一個(gè)service定義請(qǐng)求的接口
/**
* 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對(duì)象
//手動(dòng)創(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請(qǐng)求處理
// 加載框
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("無(wú)封裝:\n" + retrofitEntity.getData().toString());
}
@Override
public void onStart() {
super.onStart();
pd.show();
}
}
);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio button 按鈕 四種綁定事件的方法【實(shí)例代碼】
這篇文章主要介紹了Android studio button 按鈕 四種綁定事件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Android中關(guān)于CoordinatorLayout的一些實(shí)用布局技巧
大家都知道CoordinatorLayout是一個(gè)“加強(qiáng)版”的 FrameLayout,那么下面這篇文章主要給大家分享了Android中關(guān)于CoordinatorLayout的一些布局技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06
Flutter實(shí)現(xiàn)頂部導(dǎo)航欄功能
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)頂部導(dǎo)航欄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android組件創(chuàng)建DrawerLayout導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android組件創(chuàng)建DrawerLayout導(dǎo)航的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android中使用Kotlin實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登錄界面
Kotlin 是一種在 Java 虛擬機(jī)上運(yùn)行的靜態(tài)類型編程語(yǔ)言,被稱之為 Android 世界的Swift,由 JetBrains 設(shè)計(jì)開(kāi)發(fā)并開(kāi)源。接下來(lái)本文通過(guò)實(shí)例代碼給大家講解Android中使用Kotlin實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登錄界面,一起看看吧2017-09-09
Android Studio利用AChartEngine制作餅圖的方法
閑來(lái)無(wú)事,發(fā)現(xiàn)市面上好多app都有餅圖統(tǒng)計(jì)的功能,得空自己實(shí)現(xiàn)一下,下面這篇文章主要給大家介紹了關(guān)于Android Studio利用AChartEngine制作餅圖的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧2018-10-10
Android實(shí)現(xiàn)聲音采集回聲與回聲消除
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)聲音采集回聲與回聲消除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

