- Android开发项目框架对Retrofit+RxJava+OkHttp进行封装
先扯两句
- 本人第一次写博客如有不足请大家多多关照,由于自己在开发中需要搭建项目框架,但是现在框架很多,例如: MVC MVP MVVM等众多框架,由于本人之前搭建过MVC框架,所有提高下自己的能力,所以自己借鉴众多大神的博客自己封装了一个简单很实用的网络请求框架.好了废话不多说上代码
一,OKHttp介绍
okhttp是一个第三方类库,用于android中请求网络。
这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient(android API23 里已移除HttpClient)。
okhttp有自己的官网,官网网址:OKHttp官网
二,项目中代码
1.首先项目中需要引入依赖库:
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' /*RxAndroid*/ compile 'io.reactivex.rxjava2:rxjava:2.1.0' /*RxJava*/ compile 'com.trello.rxlifecycle2:rxlifecycle:2.1.0' compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.1.0' /*Rx生命周期管理*/ compile 'com.jakewharton:butterknife:8.6.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' /*view依赖注入*/ compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' compile 'com.squareup.okhttp3:logging-interceptor:3.8.0' compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar' compile 'com.squareup.okhttp3:okhttp:3.4.1' /*网络请求框架*/2.http包下对网络请求框架进行封装
1).ApiUtils类
import com.dayo.service.http.retrofit.RetrofitUtils; /** * 接口工具类 * 创建人:lwd * 时 间:2018/1/18 09:20 */ public class ApiUtils { private static PhoneApi phoneApi; private static UserApi userApi; public static PhoneApi getPhoneApi() { if (phoneApi == null) { phoneApi = RetrofitUtils.get().retrofit().create(PhoneApi.class); } return phoneApi; } public static UserApi getUserApi() { if (userApi == null) { userApi = RetrofitUtils.get().retrofit().create(UserApi.class); } return userApi; } }2).PhoneApi类
import com.dayo.service.http.retrofit.HttpResponse; import java.util.Map; import io.reactivex.Observable; import retrofit2.http.POST; import retrofit2.http.QueryMap; public interface PhoneApi { @POST("v1/mobile/address/query")//这里填写您的请求接口 Observable<HttpResponse> phoneQuery(@QueryMap Map<String, Object> request); }2).ApiException类
/** * api接口错误/异常统一处理类 * 异常=[程序异常,网络异常,解析异常..] * 错误=[接口逻辑错误eg:{code:-101,msg:账号密码错误}] * 创建人:lwd * 时 间:2018/1/17 14:47 */ public class ApiException extends Exception { private int code;//错误码 private String msg;//错误信息 public ApiException(Throwable throwable, int code) { super(throwable); this.code = code; } public ApiException(int code, String msg) { this.code = code; this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }3).ExceptionEngine类代码:
import com.google.gson.JsonParseException; import com.google.gson.stream.MalformedJsonException; import org.json.JSONException; import java.net.ConnectException; import java.net.SocketTimeoutException; import java.text.ParseException; import retrofit2.HttpException; /** * 错误/异常处理工具 * 创建人:lwd * 时 间:2018/1/17 15:57 */ public class ExceptionEngine { public static final int UN_KNOWN_ERROR = 1000;//未知错误 public static final int ANALYTIC_SERVER_DATA_ERROR = 1001;//解析(服务器)数据错误 public static final int ANALYTIC_CLIENT_DATA_ERROR = 1002;//解析(客户端)数据错误 public static final int CONNECT_ERROR = 1003;//网络连接错误 public static final int TIME_OUT_ERROR = 1004;//网络连接超时 public static ApiException handleException(Throwable e) { ApiException ex; if (e instanceof HttpException) { //HTTP错误 HttpException httpExc = (HttpException) e; ex = new ApiException(e, httpExc.code()); ex.setMsg("网络错误"); //均视为网络错误 return ex; } else if (e instanceof ServerException) { //服务器返回的错误 ServerException serverExc = (ServerException) e; ex = new ApiException(serverExc, serverExc.getCode()); ex.setMsg(serverExc.getMsg()); return ex; } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException || e instanceof MalformedJsonException) { //解析数据错误 ex = new ApiException(e, ANALYTIC_SERVER_DATA_ERROR); ex.setMsg("解析错误"); return ex; } else if (e instanceof ConnectException) {//连接网络错误 ex = new ApiException(e, CONNECT_ERROR); ex.setMsg("连接失败"); return ex; } else if (e instanceof SocketTimeoutException) {//网络超时 ex = new ApiException(e, TIME_OUT_ERROR); ex.setMsg("网络超时"); return ex; } else { //未知错误 ex = new ApiException(e, UN_KNOWN_ERROR); ex.setMsg("未知错误"); return ex; } } }4).ServerException类代码:
package com.dayo.service.http.exception; /** * 自定义服务器错误 * 创建人:lwd * 时 间:2018/1/17 17:07 */ public class ServerException extends RuntimeException { private int code; private String msg; public ServerException(int code, String msg) { this.code = code; this.msg = msg; } public int getCode() { return code; } public String getMsg() { return msg; } }好了代码有点多具体代码我会贴出demo请大家下载哦~