import okhttp3.logging.HttpLoggingInterceptor;
/**
-
网络API
-
@author llw
*/
public class NetworkApi {
//获取APP运行状态及版本信息,用于日志打印
private static INetworkRequiredInfo iNetworkRequiredInfo;
//OkHttp客户端
private static OkHttpClient okHttpClient;
/**
-
配置OkHttp
-
@return OkHttpClient
*/
private static OkHttpClient getOkHttpClient() {
//不为空则说明已经配置过了,直接返回即可。
if (okHttpClient == null) {
//OkHttp构建器
OkHttpClient.Builder builder = new OkHttpClient.Builder();
//设置缓存大小
int cacheSize = 100 * 1024 * 1024;
//设置OkHttp网络缓存
builder.cache(new Cache(iNetworkRequiredInfo.getApplicationContext().getCacheDir(),cacheSize));
//设置网络请求超时时长,这里设置为6s
builder.connectTimeout(6, TimeUnit.SECONDS);
//在这里添加拦截器,通过拦截器可以知道一些信息,这对于开发中是有所帮助的,后面给加上。
// …
//当程序在debug过程中则打印数据日志,方便调试用。
if (iNetworkRequiredInfo != null && iNetworkRequiredInfo.isDebug()){
//iNetworkRequiredInfo不为空且处于debug状态下则初始化日志拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
//设置要打印日志的内容等级,BODY为主要内容,还有BASIC、HEADERS、NONE。
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//将拦截器添加到OkHttp构建器中
builder.addInterceptor(httpLoggingInterceptor);
}
//OkHttp配置完成
okHttpClient = builder.build();
}
return okHttpClient;
}
}
在这里 getOkHttpClient() 方法中对OkHttp进行配置,注释已经写得很清楚了,我就也没有什么好说的。而这个里面其实还有两个日志拦截构造器没有配置上去,稍后写了之后,再添加上去。
在NetworkApi定义两个成员变量,分别用于状态API访问地址和Retrofit
//retrofitHashMap
private static HashMap<String, Retrofit> retrofitHashMap = new HashMap<>();
//API访问地址
private static String mBaseUrl;
下面写一个配置Retrofit的方法,里面的代码如下:
/**
-
配置Retrofit
-
@param serviceClass 服务类
-
@return Retrofit
*/
private static Retrofit getRetrofit(Class serviceClass) {
if (retrofitHashMap.get(mBaseUrl + serviceClass.getName()) != null) {
//刚才上面定义的Map中键是String,值是Retrofit,当键不为空时,必然有值,有值则直接返回。
return retrofitHashMap.get(mBaseUrl + serviceClass.getName());
}
//初始化Retrofit Retrofit是对OKHttp的封装,通常是对网络请求做处理,也可以处理返回数据。
//Retrofit构建器
Retrofit.Builder builder = new Retrofit.Builder();
//设置访问地址
builder.baseUrl(mBaseUrl);
//设置OkHttp客户端,传入上面写好的方法即可获得配置后的OkHttp客户端。
builder.client(getOkHttpClient());
//设置数据解析器 会自动把请求返回的结果(json字符串)通过Gson转化工厂自动转化成与其结构相符的实体Bean
builder.addConverterFactory(GsonConverterFactory.create());
//设置请求回调,使用RxJava 对网络返回进行处理
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
//retrofit配置完成
Retrofit retrofit = builder.build();
//放入Map中
retrofitHashMap.put(mBaseUrl + serviceClass.getName(), retrofit);
//最后返回即可
return retrofit;
}
RxJava是对OkHttp的请求返回做处理,那么这个就涉及到线程的切换和异常的处理。因为在实际开发中很容易出现某一个接口请求返回500、400、404之类的异常,那么也可以在这个RxJava中做处理。
先在com.llw.network包下创建一个errorhandler包,该包下创建ExceptionHandle类,用来处理异常,里面的代码如下:
package com.llw.network.errorhandler;
import android.net.ParseException;
import com.google.gson.JsonParseException;
import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;
import java.net.ConnectException;
import retrofit2.HttpException;
/**
-
异常处理
-
@author llw
*/
public class ExceptionHandle {
//未授权
private static final int UNAUTHORIZED = 401;
//禁止的
private static final int FORBIDDEN = 403;
//未找到
private static final int NOT_FOUND = 404;
//请求超时
private static final int REQUEST_TIMEOUT = 408;
//内部服务器错误
private static final int INTERNAL_SERVER_ERROR = 500;
//错误网关
private static final int BAD_GATEWAY = 502;
//暂停服务
private static final int SERVICE_UNAVAILABLE = 503;
//网关超时
private static final int GATEWAY_TIMEOUT = 504;
/**
-
处理异常
-
@param throwable
-
@return
*/
public static ResponseThrowable handleException(Throwable throwable) {
//返回时抛出异常
ResponseThrowable responseThrowable;
if (throwable instanceof HttpException) {
HttpException httpException = (HttpException) throwable;
responseThrowable = new ResponseThrowable(throwable, ERROR.HTTP_ERROR);
switch (httpException.code()) {
case UNAUTHORIZED:
case FORBIDDEN:
case NOT_FOUND:
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
default:
responseThrowable.message = “网络错误”;
break;
}
return responseThrowable;
} else if (throwable instanceof ServerException) {
//服务器异常
ServerException resultException = (ServerException) throwable;
responseThrowable = new ResponseThrowable(resultException, resultException.code);
responseThrowable.message = resultException.message;
return responseThrowable;
} else if (throwable instanceof JsonParseException
|| throwable instanceof JSONException
|| throwable instanceof ParseException) {
responseThrowable = new ResponseThrowable(throwable, ERROR.PARSE_ERROR);
responseThrowable.message = “解析错误”;
return responseThrowable;
} else if (throwable instanceof ConnectException) {
responseThrowable = new ResponseThrowable(throwable, ERROR.NETWORK_ERROR);
responseThrowable.message = “连接失败”;
return responseThrowable;
} else if (throwable instanceof javax.net.ssl.SSLHandshakeException) {
responseThrowable = new ResponseThrowable(throwable, ERROR.SSL_ERROR);
responseThrowable.message = “证书验证失败”;
return responseThrowable;
} else if (throwable instanceof ConnectTimeoutException){
responseThrowable = new ResponseThrowable(throwable, ERROR.TIMEOUT_ERROR);
responseThrowable.message = “连接超时”;
return responseThrowable;
} else if (throwable instanceof java.net.SocketTimeoutException) {
responseThrowable = new ResponseThrowable(throwable, ERROR.TIMEOUT_ERROR);
responseThrowable.message = “连接超时”;
return responseThrowable;
}
else {
responseThrowable = new ResponseThrowable(throwable, ERROR.UNKNOWN);
responseThrowable.message = “未知错误”;
return responseThrowable;
}
}
/**
- 约定异常
*/
public class ERROR {
/**
- 未知错误
*/
public static final int UNKNOWN = 1000;
/**
- 解析错误
*/
public static final int PARSE_ERROR = 1001;
/**
- 网络错误
*/
public static final int NETWORK_ERROR = 1002;
/**
- 协议出错
*/
public static final int HTTP_ERROR = 1003;
/**
- 证书出错
*/
public static final int SSL_ERROR = 1005;
/**
- 连接超时
*/
public static final int TIMEOUT_ERROR = 1006;
}
public static class ResponseThrowable extends Exception {
public int code;
public String message;
public ResponseThrowable(Throwable throwable, int code) {
super(throwable);
this.code = code;
}
}
public static class ServerException extends RuntimeException {
public int code;
public String message;
}
}
里面都是一些常规的错误,相信你可能碰到过一些。
然后再建一个HttpErrorHandler类,代码如下:
package com.llw.network.errorhandler;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
/**
- 网络错误处理
*/
public class HttpErrorHandler implements Function<Throwable, Observable> {
/**
-
处理以下两类网络错误:
-
1、http请求相关的错误,例如:404,403,socket timeout等等;
-
2、应用数据的错误会抛RuntimeException,最后也会走到这个函数来统一处理;
*/
@Override
public Observable apply(Throwable throwable) throws Exception {
//通过这个异常处理,得到用户可以知道的原因
return Observable.error(ExceptionHandle.handleException(throwable));
}
}
还需要写一个基础返回类,在com.llw.network下新建一个BaseResponse,代码如下:
package com.llw.network;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
-
基础返回类
-
@author llw
*/
public class BaseResponse {
//返回码
@SerializedName(“res_code”)
@Expose
public Integer responseCode;
//返回的错误信息
@SerializedName(“res_error”)
@Expose
public String responseError;
}
现在准备工作都做好了,下面就要写这个RxJava的配置了,不过还有一步就是,在NetworkApi中写一个错误码的处理方法