MVVM在网络中封装(OkHttp+Retrofit+Gson+RxJava2)

封装目录结构

1.首先安装类库

    api 'com.squareup.okhttp3:okhttp:3.11.0'
    api 'com.zhy:okhttputils:2.6.2'
    //Rxlifecycle
    api 'com.trello:rxlifecycle:0.3.1'
    api 'com.trello:rxlifecycle-components:0.3.1'
    api 'com.github.bumptech.glide:glide:4.8.0'
    api 'com.squareup.retrofit2:converter-gson:2.+'
    //必须使用
    api 'com.lzy.net:okgo:3.0.4'
    api 'com.squareup.okio:okio:1.5.0'
    api 'io.reactivex.rxjava2:rxjava:2.2.2'
    api 'io.reactivex.rxjava2:rxandroid:2.1.0'
    api 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.+'
    api 'com.squareup.retrofit2:retrofit:2.+'
    api 'com.squareup.okhttp3:logging-interceptor:3.11.0'
    api 'com.squareup.retrofit2:converter-gson:2.+'
    api 'com.squareup.retrofit2:converter-scalars:2.0.0'
    api 'com.facebook.stetho:stetho:1.4.2'
    api 'com.facebook.stetho:stetho-okhttp3:1.4.2'
    // 依赖以下两个库,会自动引用基础库与Android库
    api 'com.trello.rxlifecycle2:rxlifecycle-components:2.1.0'
    api 'com.trello.rxlifecycle2:rxlifecycle-navi:2.1.0'
    api 'com.tbruyelle.rxpermissions:rxpermissions:0.7.0@aar'
    api 'com.jakewharton.rxbinding:rxbinding:1.+'
    // Lifecycles, LiveData 和 ViewModel
    //implementation "android.arch.lifecycle:runtime:1.0.0-alpha5"
    api "android.arch.lifecycle:extensions:1.1.0"
    // 对 RxJava 的支持
    api "android.arch.persistence.room:rxjava2:1.0.0-alpha5"

2.创建常量类

[——建议将这个类放在config文件夹——]

package com.ylink.https.config;

/**
 * 全局常量
 *
 * @author twilight
 * @Time 2019-07-21
 */

public class HttpsConstant {
    /**
     * 请求后台的所有API接口都在这里配置;统一管理
     */
    public static class Server {
        //服务器超时时间 16 秒
        public final static int TIME_OUT = 30;
        //测试服务器地址
        public static final String ROOT_URL = "https://host:port/mobile-adapter/rest/merchant/app/"; //payapisit.fuxunpay.com
//        public static final String ROOT_URL = "http://host:port/hibi-mobile-adapter/rest/merchant/app/";
    }

    /**
     * 服务器返回的状态码
     */
    public static class RespCode {
        //处理成功
        public static final String R000 = "R000";
        //登录超时
        public static final String R097 = "R097";
        //令牌无效
        public static final String R098 = "R098";
    }
    /**
     * 全局静态变量
     */
    public static class Param {

    }

    /**
     * sp 常量
     */
    public static class SP {

        public static final String SP = "sp";//sp
        public static final String SESSION_ID = "session_id";//
    }
    /**
     * 全局常量
     */
    public static class FINALVALUE {

    }
}

3.创建错误处理类

[——建议将这个类放在exception文件夹——]
package com.ylink.https.exception;

/**
 * 前端自定义Exception
 */
public class ApiException extends Exception {
    private String statusCode;//错误码
    private String statusDesc;//错误信息

    public ApiException(Throwable throwable, String statusCode) {
        super(throwable);
        this.statusCode = statusCode;
    }

    public ApiException(String statusCode, String statusDesc) {
        this.statusCode = statusCode;
        this.statusDesc = statusDesc;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }
}
package com.ylink.https.exception;

/**
 * 服务器返回的Exception
 */
public class ServerException extends RuntimeException {
    private String statusCode;//错误码
    private String statusDesc;//错误信息

    public ServerException(String statusCode, String statusDesc) {
        this.statusCode = statusCode;
        this.statusDesc = statusDesc;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }
}
package com.ylink.https.exception;

import android.net.ParseException;
import android.util.MalformedJsonException;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.HttpException;

public class ExceptionEngine {
    //客户端报错
    public static final int UN_KNOWN_ERROR = 9000;//未知错误
    public static final int ANALYTIC_SERVER_DATA_ERROR = 9001;//解析(服务器)数据错误
    public static final int ANALYTIC_CLIENT_DATA_ERROR = 9002;//解析(客户端)数据错误
    public static final int CONNECT_ERROR = 9003;//网络连接错误
    public static final int TIME_OUT_ERROR = 9004;//网络连接超时
    public static final int UNKNOWNHOSTEXCEPTION = 9005;//网络连接超时

    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (e instanceof HttpException) {             //HTTP错误
            HttpException httpExc = (HttpException) e;
            ex = new ApiException(e, String.valueOf(httpExc.code()));
            ex.setStatusDesc("网络错误,请稍后再试");  //均视为网络错误
            return ex;
        } else if (e instanceof ServerException) {    //服务器返回的错误
           ServerException serverExc = (ServerException) e;
            ex = new ApiException(serverExc, serverExc.getStatusCode());
            ex.setStatusDesc(serverExc.getStatusDesc());
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException || e instanceof MalformedJsonException) {  //解析数据错误
            ex = new ApiException(e, String.valueOf(ANALYTIC_SERVER_DATA_ERROR));
            ex.setStatusDesc("客户端异常,请稍后再试");
            return ex;
        } else if (e instanceof ConnectException) {//连接网络错误
            ex = new ApiException(e, String.valueOf(CONNECT_ERROR));
            ex.setStatusDesc("网络连接错误,请稍后再试");
            return ex;
        } else if (e instanceof SocketTimeoutException) {//网络超时
            ex = new ApiException(e, String.valueOf(TIME_OUT_ERROR));
            ex.setStatusDesc("网络连接超时,请稍后再试");
            return ex;
        }
        else if (e instanceof UnknownHostException) {//网络异常
            ex = new ApiException(e, String.valueOf(UNKNOWNHOSTEXCEPTION));
            ex.setStatusDesc("网络异常,请检查您的网络连接");
            return ex;
        }
        else {  //未知错误
            ex = new ApiException(e, String.valueOf(UN_KNOWN_ERROR));
            ex.setStatusDesc("系统异常,请稍后再试");
            return ex;
        }
    }

}

4.创建DTO、VO、Subscriber、Repository基类

[——建议将这些类放在base文件夹——]
package com.ylink.https.base;

/**
 * 服务器返回公共实体
 *
 * @param <T>
 * @author twilight
 * @Time 2019-07-21
 */
public class BaseDto<T> {
    private String statusCode;
    private String statusDesc;
    private T data;

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
package com.ylink.https.base;

import java.io.Serializable;

/**
 * 请求服务器的公共参数
 * 如果有需要可以在这添加公共参数
 *
 * @author twilight
 * @Time 2019-07-21
 */
public class BaseVo implements Serializable {

}
package com.ylink.https.base;

import android.arch.lifecycle.MutableLiveData;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;

import com.ylink.commons.CommonsContext;
import com.ylink.commons.config.CommonsConstant;
import com.ylink.commons.utils.AppManager;
import com.ylink.https.config.HttpsConstant;
import com.ylink.https.exception.ApiException;
import com.ylink.https.exception.ExceptionEngine;
import com.ylink.https.exception.ServerException;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

/**
 * 自定义请服务器被观察者
 *
 * @author twilight
 * @Time 2019-07-21
 */
public class BaseHttpSubscriber<T> implements Subscriber<BaseDto<T>> {

    //异常类
    private ApiException ex;

    public BaseHttpSubscriber() {
        data = new MutableLiveData();
    }

    private MutableLiveData<BaseDto<T>> data;

    public MutableLiveData<BaseDto<T>> get() {
        return data;
    }

    public void set(BaseDto<T> t) {
        this.data.setValue(t);
    }

    public void onFinish(BaseDto<T> t) {
        set(t);
    }

    @Override
    public void onSubscribe(Subscription s) {
        // 观察者接收事件 = 1个
        s.request(1);
    }

    @Override
    public void onNext(BaseDto<T> t) {
        if (t.getStatusCode().equals(HttpsConstant.RespCode.R000)) {
            onFinish(t);
        } else if(t.getStatusCode().equals(HttpsConstant.RespCode.R097) || //登录超时,重新登录
                t.getStatusCode().equals(HttpsConstant.RespCode.R098)){ //令牌无效
            CommonsContext.getInstance().reLogin();
            AppManager.getInstance().finishAllActivity();//结束所有页面
        } else{
            ex = ExceptionEngine.handleException(new ServerException(t.getStatusCode(), t.getStatusDesc()));
            getErrorDto(ex);
        }
    }

    @Override
    public void onError(Throwable t) {
        ex = ExceptionEngine.handleException(t);
        getErrorDto(ex);
    }

    /**
     * 初始化错误的dto
     *
     * @param ex
     */
    private void getErrorDto(ApiException ex) {
        BaseDto dto = new BaseDto();
        dto.setStatusCode(ex.getStatusCode());
        dto.setStatusDesc(ex.getStatusDesc());
        onFinish((BaseDto<T>) dto);
    }

    @Override
    public void onComplete() {
    }

}
package com.ylink.https.base;


import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Repository基类
 *
 * @author twilight
 * @Time 2019-07-21
 */

public class BaseRepository {


    /**
     * 请求网络
     * @param flowable
     * @param <T>
     * @return
     */
    public <T> BaseHttpSubscriber<T> request(Flowable<BaseDto<T>> flowable){
        BaseHttpSubscriber<T> baseHttpSubscriber = new BaseHttpSubscriber<>(); //RxJava Subscriber回调
        flowable.subscribeOn(Schedulers.io()) //解决背压
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(baseHttpSubscriber);
        return baseHttpSubscriber;
    }
}

5.创建拦截器

[——建议将这个类放在interceptor文件夹——]
package com.ylink.https.interceptor;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.ylink.commons.utils.SPUtil;
import com.ylink.https.config.HttpsConstant;

import java.io.IOException;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 自定义拦截器刷新sessionId  非首次请求的处理
 * @author twilight
 * @Time 2019-07-21
 */
public class AddCookiesInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        String cookieStr = SPUtil.getData(HttpsConstant.SP.SP, HttpsConstant.SP.SESSION_ID, String.class, null);
        List<String> cookies = JSONObject.parseArray(cookieStr, String.class);
        if (cookies != null) {
            for (String cookie : cookies) {
                builder.addHeader("Cookie", cookie);
                Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
            }
        }
        return chain.proceed(builder.build());
    }
}
package com.ylink.https.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.ylink.commons.utils.SPUtil;
import com.ylink.https.config.HttpsConstant;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * 自定义拦截器刷新sessionId 首次请求的处理
 * @author twilight
 * @Time 2019-07-21
 */
public class ReceivedCookiesInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Response originalResponse = chain.proceed(chain.request());
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            List<String> cookies = new ArrayList<>();
            for (String header : originalResponse.headers("Set-Cookie")) {
                cookies.add(header);
            }
            String cookieStr = JSONObject.toJSONString(cookies);
            SPUtil.putData(HttpsConstant.SP.SP, HttpsConstant.SP.SESSION_ID, cookieStr);
        }

        return originalResponse;
    }
}

6.创建请求类

[——建议将这个类放在http文件夹——]
package com.ylink.https.http;

import android.util.Log;

import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.ylink.https.config.HttpsConstant;
import com.ylink.https.interceptor.AddCookiesInterceptor;
import com.ylink.https.interceptor.ReceivedCookiesInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 普通的网络请求用到的Retrofit
 */

public class RequetRetrofit {
    private static final String TAG = "RequetRetrofit";
     /**
     * 创建okhttp相关对象
     */
    private static OkHttpClient okHttpClient;
    /**
     * 创建Retrofit相关对象
     */
    private static Retrofit retrofit;

    public static <T> T getInstance(final Class<T> service) {
        if (okHttpClient == null) {
            synchronized (RequetRetrofit.class) {
                if(okHttpClient == null) {
                    /**
                     * 创建okhttp相关对象
                     */
                    okHttpClient = new OkHttpClient.Builder()

                            .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                                @Override
                                public void log(String message) {   //访问网络请求,和服务端响应请求时。将数据拦截并输出
                                    Log.e(TAG, "log: " + message);
                                }
                            }).setLevel(HttpLoggingInterceptor.Level.BODY))     //Log等级
                            .connectTimeout(HttpsConstant.Server.TIME_OUT, TimeUnit.SECONDS)       //超时时间
                            .readTimeout(HttpsConstant.Server.TIME_OUT, TimeUnit.SECONDS)
                            .writeTimeout(HttpsConstant.Server.TIME_OUT, TimeUnit.SECONDS)
                            .addNetworkInterceptor(new StethoInterceptor())
                            .addInterceptor(new AddCookiesInterceptor()) //
                            .addInterceptor(new ReceivedCookiesInterceptor())
                            .build();
                }
            }
        }

        if (retrofit == null) {
            synchronized (RequetRetrofit.class) {
                if(retrofit == null) {
                    retrofit = new Retrofit.Builder()
                            .baseUrl(HttpsConstant.Server.ROOT_URL)         //BaseUrl
                            .client(okHttpClient)                       //请求的网络框架
                            .addConverterFactory(GsonConverterFactory.create())     //解析数据格式
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 使用RxJava作为回调适配器
                            .build();
                }
            }
        }
        return retrofit.create(service);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容