接着来,本系列的第二篇,就是重中之重啦,网络层的封装。第一篇是:Android-多列表的项目(Rxjava+Rtrofit+Recyclerview+Glide+Adapter封装)之(一)项目架构
本文章结构:1.网络层封装涉及的东西;2.如何封装网络层;3.如何使用封装了的网络层。
一、网络层封装涉及的东西
大众方案:OkHttp+Rxjava+Rtrofit
介绍: Retrofit和okHttp师出同门,也是Square的开源库,它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底:比方说通过注解来配置请求参数,通过工厂来生成CallAdapter,Converter,你可以使用不同的请求适配器(CallAdapter), 比方说RxJava,Java8, Guava。你可以使用不同的反序列化工具(Converter),比方说json, protobuff, xml, moshi等等。
官网 http://square.github.io/retrofit/
github https://github.com/square/retrofit
Rxjava:http://gank.io/post/560e15be2dca930e00da1083
二、如何封装网络层
1.导入
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
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'
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0'
2.RetrofitUtil实现Retrofit单例:
public class RetrofitUtil {
private static final String TAG = "retrofit";
private static final String BASE_URL = "https://api.douban.com";
private static final int DEFAULT_TIMEOUT = 5;
private static Retrofit retrofit;
private RetrofitUtil(){
}
public static Retrofit getInstance(){
if(retrofit==null) {
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(App.getInstance()));
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
File cacheFile = new File(App.getInstance().getCacheDir(), "superman");
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100);
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()
.client(httpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.build();
}else{
return retrofit;
}
}
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;
}
};
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())){
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
Logger.t(TAG).w("no network");
}
Response originalResponse = chain.proceed(request);
if(NetUtil.isConnected(App.getInstance())){
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>
三、如何使用封装的网络层:
1.输入主机地址,编写接口:
public interface MovieRequest {
@GET("/v2/movie/top250")
Observable getMovies(@Query("start") int start, @Query("count") int count);
}
2.使用postman等工具先查看那个数据,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 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 genres;
public List casts;
public List 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.但是呢我们经常看到一个这么大的json,有些数据我们是不会用到的嘛,所以我们可以写多一个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 datas = new ArrayList<>();
private CommonAdapter 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() {
@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()
ToastUtils.showToast(getActivity(), getString(R.string.request_success))
for (int i = 0
datas.add(new AbilityItem(abilityBean.subjects.get(i).images.medium, abilityBean.subjects.get(i).title, abilityBean.subjects.get(i).year))
bitmapList.add(abilityBean.subjects.get(i).images.large)
}
loadConvenientBanner()
//下面是设置适配器,对应view与数据
adapter = new CommonAdapter(getActivity(), R.layout.item_fragment_ability_recyclerview, datas) {
@Override
protected void convert(ViewHolder holder, final AbilityItem abilityItem, final int position) {
//使用这个封装好的adapter模式去设置好一系列的view,与数据对应上
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.请求不成功执行onError;2.请求成功,但是onNext解析过程出现错误,同样执行onError。
对了,还有一个问题!!!!我回来补充啦!!!
就是retrofit我在项目架构时,想尝试写一个父的请求接口去规范各个请求接口的,但是呢!!!retrofit的源码是不支持这样的父接口的!!!!它反射不出来的!!!
所以我们使用retrofit的时候就要注意了,只能是单独出来的接口不能使用子接口继承父接口的方式!!!
源码下载:Android-多列表的项目Rxjava+Rtrofit+Recyclerview+Glide+Adapter封装
好了,Android-多列表的项目(Rxjava+Rtrofit+Recyclerview+Glide+Adapter封装)之(二)网络层的封装讲完了。本博客是这个系列的第二篇,所以讲得是网络层的封装。另外,这个系列还有一些我在外包项目过程中做的优化,以及一些发布签名等等技巧,我会尽快出完给大家,分享经验给大家。欢迎在下面指出错误,共同学习!!你的点赞是对我最好的支持!!
更多内容,可以访问JackFrost的博客