本文章結構:1.網(wǎng)絡層封裝涉及的東西;2.如何封裝網(wǎng)絡層;3.如何使用封裝了的網(wǎng)絡層。
一、網(wǎng)絡層封裝涉及的東西
大眾方案:OkHttp+Rxjava+Rtrofit
介紹: Retrofit和okHttp師出同門,也是Square的開源庫,它是一個類型安全的網(wǎng)絡請求庫,Retrofit簡化了網(wǎng)絡請求流程,基于OkHtttp做了封裝,解耦的更徹底:比方說通過注解來配置請求參數(shù),通過工廠來生成CallAdapter,Converter,你可以使用不同的請求適配器(CallAdapter), 比方說RxJava,Java8, Guava。你可以使用不同的反序列化工具(Converter),比方說json, protobuff, xml, moshi等等。
二、如何封裝網(wǎng)絡層
1.導入
// https://github.com/ReactiveX/RxAndroid rx異步
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
// https://github.com/square/retrofit 網(wǎng)絡請求
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
//https://github.com/franmontiel/PersistentCookieJar Okhttp的Cookie緩存
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0'
2.RetrofitUtil實現(xiàn)Retrofit單例:
public class RetrofitUtil {
private static final String TAG = "retrofit";
//TODO:修改主機地址
private static final String BASE_URL = "https://api.douban.com";
private static final int DEFAULT_TIMEOUT = 5;
private static Retrofit retrofit;
//實例化私有
private RetrofitUtil(){
}
//單例Retrofit
public static Retrofit getInstance(){
if(retrofit==null) {
//這里有個很關鍵的地方,就是傳入App的上下文啦。
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(App.getInstance()));
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); //okhttp創(chuàng)建,寫入緩存機制,還有addInterceptor
httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
File cacheFile = new File(App.getInstance().getCacheDir(), "superman");
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb。這里開始就是緩存設置啦。實現(xiàn)一鍵緩存以及請求頭的配置等等
httpClientBuilder.cache(cache);
httpClientBuilder.cookieJar(cookieJar);
httpClientBuilder.addInterceptor(LoggingInterceptor);
httpClientBuilder.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR);
httpClientBuilder.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR);
return new Retrofit.Builder() //retrofit的創(chuàng)建。
.client(httpClientBuilder.build()) //傳入okhttp
.addConverterFactory(GsonConverterFactory.create()) //傳入gson解析手段
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //傳入異步手段
.baseUrl(BASE_URL) //傳入服務器地址
.build();
}else{
return retrofit;
}
}
//okHttp的攔截器
private static final Interceptor LoggingInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Logger.t(TAG).i(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Logger.t(TAG).i(String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
};
//okHttp的攔截器
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if(!NetUtil.isConnected(App.getInstance())){//判斷是否有網(wǎng)絡再進行操作
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
Logger.t(TAG).w("no network");
}
//請求頭緩存控制
Response originalResponse = chain.proceed(request);
if(NetUtil.isConnected(App.getInstance())){
//有網(wǎng)的時候讀接口上的@Headers里的配置,你可以在這里進行統(tǒng)一的設置
String cacheControl = request.cacheControl().toString();
return originalResponse.newBuilder()
.header("Cache-Control", cacheControl)
.removeHeader("Pragma")
.build();
}else{
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=2419200")
.removeHeader("Pragma")
.build();
}
}
};
}
3.調用前關鍵配置:
一個app的類獲取app的上下文,以及其關鍵配置
public class App extends Application {
private static App app;
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
}
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
app = this;
}
public static App getInstance() {
return app;
}
}
//這里關鍵是要注冊這個app的上下文
<application
android:name=".App" //也就是這里啦
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:allowBackup="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
三、如何使用封裝的網(wǎng)絡層:
1.輸入主機地址,編寫接口:
public interface MovieRequest {
// //請求參數(shù),注意這個接口要跟bean完全對應,根據(jù)第二點來操作就對了
@GET("/v2/movie/top250")
Observable<AbilityBean> getMovies(@Query("start") int start, @Query("count") int count);
}
2.使用postman等工具先查看那個數(shù)據(jù),json。然后使用gsonformat,生成那個bean。GsonFormat的使用
package com.fuzhucheng.rxjavartrofitrecyclerview.bean;
import java.util.List;
/**
* Created by 符柱成 on 2016/12/14.
*/
public class AbilityBean {
public int image;
public String place;
public int like;
public int price;
public int count;
public int start;
public int total;
public String title;
public List<SubjectsEntity> subjects;
public AbilityBean(int image, String place, int like, int price) {
this.image = image;
this.place = place;
this.like = like;
this.price = price;
}
public static class SubjectsEntity {
public RatingEntity rating;
public String title;
public int collect_count;
public String original_title;
public String subtype;
public String year;
public RatingEntity.ImagesEntity images;
public String alt;
public String id;
public List<String> genres;
public List<RatingEntity.CastsEntity> casts;
public List<RatingEntity.DirectorsEntity> directors;
public static class RatingEntity {
public int max;
public double average;
public String stars;
public int min;
public static class ImagesEntity {
public String small;
public String large;
public String medium;
}
public static class CastsEntity {
public String alt;
public AvatarsEntity avatars;
public String name;
public String id;
public static class AvatarsEntity {
public String small;
public String large;
public String medium;
}
}
public static class DirectorsEntity {
public String alt;
public AvatarsEntityX avatars;
public String name;
public String id;
public static class AvatarsEntityX {
public String small;
public String large;
public String medium;
}
}
}
}
}
3.但是呢我們經(jīng)??吹揭粋€這么大的json,有些數(shù)據(jù)我們是不會用到的嘛,所以我們可以寫多一個bean去對應我們的客戶端嘛。
如果我們的客戶端一個列表只需這么多信息,那就寫個這么小的bean就足夠了嘛。
public class AbilityItem {
private String image;
private String title;
private String content;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public AbilityItem(String image, String title, String content) {
this.image = image;
this.title = title;
this.content = content;
}
}
4.使用
private List<AbilityItem> datas = new ArrayList<>();//為列表保存bean
private CommonAdapter<AbilityItem> adapter;//列表的適配器
private MovieRequest movieRequest;//請求的接口
//這個方法放進loadData加載即可
private void getMovie() {
movieRequest = RetrofitUtil.getInstance().create(MovieRequest.class);//使用我們的工具類,反射得到請求體
//下面是rxjava的格式啦
movieRequest.getMovies(0, 10)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<AbilityBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
ToastUtils.showToast(getActivity(), getString(R.string.request_error));
}
@Override
public void onNext(AbilityBean abilityBean) {
swipeRefreshLayout.setRefreshing(false);//這個是下拉刷新的
datas.clear();//避免數(shù)據(jù)重復,清楚數(shù)據(jù)
ToastUtils.showToast(getActivity(), getString(R.string.request_success));
for (int i = 0; i < abilityBean.subjects.size(); i++) {
datas.add(new AbilityItem(abilityBean.subjects.get(i).images.medium, abilityBean.subjects.get(i).title, abilityBean.subjects.get(i).year));//列表的數(shù)據(jù)
bitmapList.add(abilityBean.subjects.get(i).images.large);//那個輪播圖的數(shù)據(jù)
}
loadConvenientBanner();//從父類繼承得來的方法
//下面是設置適配器,對應view與數(shù)據(jù)
adapter = new CommonAdapter<AbilityItem>(getActivity(), R.layout.item_fragment_ability_recyclerview, datas) {
@Override
protected void convert(ViewHolder holder, final AbilityItem abilityItem, final int position) {
//使用這個封裝好的adapter模式去設置好一系列的view,與數(shù)據(jù)對應上
holder.setText(R.id.place, abilityItem.getTitle());
holder.setText(R.id.price, abilityItem.getContent());
ImageView circleImageView = holder.getView(R.id.image);
Glide.with(getActivity()).load(abilityItem.getImage()).into(circleImageView);
}
};
recyclerView.setAdapter(adapter);
}
});
}
其中的一個調試原理:Rtrofit做好的封裝是:1.請求不成功執(zhí)行onError;2.請求成功,但是onNext解析過程出現(xiàn)錯誤,同樣執(zhí)行onError。
好了,Android-多列表的項目(Rxjava+Rtrofit+Recyclerview+Glide+Adapter封裝)之(二)網(wǎng)絡層的封裝講完了。本博客是這個系列的第二篇,所以講得是網(wǎng)絡層的封裝。另外,這個系列還有一些我在外包項目過程中做的優(yōu)化,以及一些發(fā)布簽名等等技巧,我會盡快出完給大家,分享經(jīng)驗給大家。歡迎在下面指出錯誤,共同學習??!你的點贊是對我最好的支持!!