仿“ONE”项目运用

作者: 泅渡者 | 来源:发表于2017-03-29 11:29 被阅读97次

简介

在看这篇文章时最好先去我之前写的有关DataBinding、RxJava、Retorfit2.0的介绍,本项目数据来源是https://github.com/jokermonn/-Api/blob/master/ONEv3.5.0~.md
若该API存在侵权行为,希望及时通知,本人会立刻关闭该项目以及所有有关文章,谢谢。
本项目的地址:ONE
https://github.com/VampireCarrot/One.git
项目截图:

图片.png 图片.png 图片.png
项目架构

我们先看一眼项目的整体结构


图片.png

接下来我会带大家了解下我在项目中是怎么运用Retorfit +MVP的:

HTTP 封装
图片.png

API:

public class Api {
    /**
     * 服务器地址
     */
    private static final String BASE_URL = "http://v3.wufazhuce.com:8000/api/";
    // 消息头
    private static final String HEADER_X_HB_Client_Type = "X-HB-Client-Type";
    private static final String FROM_ANDROID = "ayb-android";
    private static ApiService service;
    private static Retrofit retrofit;
    private static OkHttpClient okHttpClient;
    public static ApiService getService() {
        if (service == null) {
            service = getRetrofit().create(ApiService.class);
        }
        return service;
    }
    private static Retrofit getRetrofit() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .client(getOkHttpClient())
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }
    public static OkHttpClient getOkHttpClient(){
        if (okHttpClient==null){
            okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .addInterceptor(getLogingInstance())
                    .cache(getCacheInstance())
                    .build();
        }
        return okHttpClient;
    }
    public static HttpLoggingInterceptor getLogingInstance(){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                LogUtil.g("HttpLog", message);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return interceptor;
    }
    public static Cache getCacheInstance(){
        //设置 请求的缓存
        File cacheFile = new File(MyApplication.getInstance().getCacheDir(), "cache");
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50); //50Mb
        return cache;
    }
    /**
     * 对 Observable<T> 做统一的处理,处理了线程调度、分割返回结果等操作组合了起来
     * @param responseObservable
     * @param <T>
     * @return
     */
    protected <T > Observable<T> applySchedulers(Observable<T> responseObservable) {
        return responseObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new Func1<T, Observable<T>>() {
                    @Override
                    public Observable<T> call(T tResponse) {
                        return flatResponse(tResponse);
                    }
                });
    }
    /**
     * 对网络接口返回的Response进行分割操作 对于json 解析错误以及返回的 响应实体为空的情况
     * @param response
     * @return
     */
    public < T > Observable<T> flatResponse(final T response) {
        return Observable.create(new Observable.OnSubscribe<T>() {
            @Override
            public void call(Subscriber<? super T> subscriber) {
                if (response != null) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(response);
                    }
                } else {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onError(new APIException("自定义异常类型", "解析json错误或者服务器返回空的json"));
                    }
                    return;
                }
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onCompleted();
                }
            }
        });
    }

    /**
     * 自定义异常
     */
    public static class APIException extends Exception {
        public String code;
        public String message;

        public APIException(String code, String message) {
            this.code = code;
            this.message = message;
        }

        @Override
        public String getMessage() {
            return message;
        }

    }
}

在Api中我们将 OkHttpClient、HttpLoggingInterceptor 、Retrofit 等进行初始化操作。都采用单例模式,方便调用以及销毁。
至于applySchedulers其实就是RXJava的内容了,这里主要是统一处理线程调度,让消息发送者处在Schedulers.io()也就是异步线程,让消息接收者处在AndroidSchedulers.mainThread()也就是主线程。这样我们使用Retorfit请求得来的数据便可以在主线程做处理。

ApiService:

定义API

public interface ApiService {

    @GET("onelist/idlist")
    Observable<IdBean> doIdList(@Query("channel") String channel, @Query("version") String version, @Query("uuid") String uuid, @Query("platform") String platform);

    @GET("onelist/{idList}/0?channel=wdj&version=4.0.2&uuid=ffffffff-a90e-706a-63f7-ccf973aae5ee&platform=android")
    Observable<OneListBean> doOneList(@Path("idList") String idList);

    @GET("essay/{item_id}/?channel=wdj&source=summary&source_id=9261&version=4.0.2&uuid=ffffffff-a90e-706a-63f7-ccf973aae5ee&platform=android")
    Observable<OneReadInfo> doOneReadInfo(@Path("item_id") String item_id);
}

图片.png

这张图完全阐述了该项目中retorfit +rxJava 间是怎么使用的。(绘图比较潦草请见谅)
在在项目中我只完成了首页页面,以及部分子页面的展示功能。其他功能还有待完善。大家可以clone下来好好研究下 。

相关文章

  • 仿“ONE”项目运用

    简介 在看这篇文章时最好先去我之前写的有关DataBinding、RxJava、Retorfit2.0的介绍,本项...

  • 英美名篇第一季13

    【仿写】 No one can say that a love with encounter,acquaintan...

  • ReactNative 仿美团项目

    ReactNative 仿美团项目 ReactNative 仿美团项目

  • ios-html-bootStrap运用

    综合演练:高仿Mac桌面 6.1 功能 仿Mac桌面的布局,并加以运用 菜单和Dom的运用 6.2 技术要点 字体...

  • 项目one

    项目正式环境地址:https://my.baocloud.cn r02037/qwe123/https://my....

  • iOS日历样式的滚动Label

    最近在写iOS高仿"一个-ONE"App这个项目的时候,由于需要实现一个需求:显示日期的label通过动画的形式上...

  • ReactNative仿《ONE》APP

    仿《ONE》APP又来了! 又写了一个《ONE》,别急呀,我可没copy上次写的代码~ 这是用ReactNativ...

  • Harry Day 65

    1.fungus n. 真菌,霉菌;菌类 仿White fungus is one of main edible ...

  • 一个开源的练手项目《TheOne》

    1、前言 前不久在简书上看到一哥们写的仿《一个ONE》APP,看了之后觉得这个项目还挺不错的,又加上自己还没怎么正...

  • 高仿百思不得姐项目

    高度仿写百思不得姐项目,实现精华,新帖,发布,关注,我的五大功能模块,运用了很多技术。 使用技术:MVC设计模式,...

网友评论

    本文标题:仿“ONE”项目运用

    本文链接:https://www.haomeiwen.com/subject/qbreottx.html