Android-多列表的项目(Rxjava+Rtrofit+Recyclerview+Glide+Adapter封装)之(二)网络层的封装

接着来,本系列的第二篇,就是重中之重啦,网络层的封装。第一篇是: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.导入

// 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  网络请求
    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实现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创建,写入缓存机制,还有addInterceptor
            httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
            File cacheFile = new File(App.getInstance().getCacheDir(), "superman");
            Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb。这里开始就是缓存设置啦。实现一键缓存以及请求头的配置等等
            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的创建。
                    .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())){//判断是否有网络再进行操作
                request = request.newBuilder()
                        .cacheControl(CacheControl.FORCE_CACHE)
                        .build();
                Logger.t(TAG).w("no network");
            }
            //请求头缓存控制
            Response originalResponse = chain.proceed(request);
            if(NetUtil.isConnected(App.getInstance())){
                //有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置
                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 {
//    //请求参数,注意这个接口要跟bean完全对应,根据第二点来操作就对了
    @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<>();//为列表保存bean
    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; 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));//列表的数据
                            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的博客

你可能感兴趣的:(Android)