前言
一直以来想自己封装一套合适的联网请求框架,所以查询各种资料踩过各种坑之后鼓捣出这一套框架,贴出来希望大家多多指点,共同进步
1,依赖
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.3'
compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.1'
2,网络请求部分
package com.hmkj.retrofitrxjavalib.http;
import com.hmkj.retrofitrxjavalib.retrofitservice.ApiService;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
public class RetrofitFactory {
//服务器主机地址
static String URL = "https://www.baidu.com";
private static RetrofitFactory mFactory;
private static final String TAG = "RetrofitFactory";
private Retrofit mRetrofit;
private RetrofitFactory() {
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.readTimeout(10, TimeUnit.SECONDS);
builder.writeTimeout(10,TimeUnit.SECONDS);
builder.connectTimeout(10, TimeUnit.SECONDS);
//拦截器添加 见文章第8条
builder.addInterceptor(new CommonInterceptor());
OkHttpClient client = builder.build();
//自定义MyGsonConverterFactory 见文章第3条
mRetrofit = new Retrofit.Builder().baseUrl(URL)
.client(client)
.addConverterFactory(MyGsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
public static RetrofitFactory getFactory() {
if (mFactory != null) return mFactory;
synchronized (TAG) {
if (mFactory != null) return mFactory;
mFactory = new RetrofitFactory();
}
return mFactory;
}
public ApiService createService(){
return mRetrofit.create(ApiService.class);
}
}
3,自定义Converter 重写三个相关类
1.重写 GsonConverterFactory
package com.hmkj.retrofitrxjavalib.http;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:[email protected]
*/
public class MyGsonConverterFactory extends Converter.Factory {
private final Gson gson;
private MyGsonConverterFactory(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
this.gson = gson;
}
public static MyGsonConverterFactory create() {
return create(new Gson());
}
public static MyGsonConverterFactory create(Gson gson) {
return new MyGsonConverterFactory(gson);
}
@Override
public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
TypeAdapter> adapter = gson.getAdapter(TypeToken.get(type));
return new MyGsonResponseBodyConverter<>(gson, adapter);
}
@Override
public Converter, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
TypeAdapter> adapter = gson.getAdapter(TypeToken.get(type));
return new MyGsonRequestBodyConverter<>(gson, adapter);
}
}
2.重写 GsonRequestBodyConverter
package com.hmkj.retrofitrxjavalib.http;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import retrofit2.Converter;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:[email protected]
*/
public class MyGsonRequestBodyConverter implements Converter {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private static final Charset UTF_8 = Charset.forName("UTF-8");
private final Gson gson;
private final TypeAdapter adapter;
public MyGsonRequestBodyConverter(Gson gson, TypeAdapter adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override
public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
adapter.write(jsonWriter, value);
jsonWriter.close();
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
}
3.重写 GsonResponseBodyConverter
package com.hmkj.retrofitrxjavalib.http;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.hmkj.retrofitrxjavalib.data.BaseInfo;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import retrofit2.Converter;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:[email protected]
*/
public class MyGsonResponseBodyConverter implements Converter {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private final Gson mGson;
private final TypeAdapter adapter;
public MyGsonResponseBodyConverter(Gson gson, TypeAdapter adapter) {
mGson = gson;
this.adapter = adapter;
}
@Override
public T convert(ResponseBody value) throws IOException {
String response = value.string();
BaseInfo baseInfo = mGson.fromJson(response, BaseInfo.class);
//关注的重点,自定义响应码中非0的情况,一律抛出ApiException异常。
//这样,我们就成功的将该异常交给onError()去处理了。
if (baseInfo.getCode() != ApiErrorCode.SUCCUSE_CLIENT) {
value.close();
throw new ApiException(baseInfo.getCode(), baseInfo.getMsg());
}
MediaType mediaType = value.contentType();
Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
ByteArrayInputStream bis = new ByteArrayInputStream(response.getBytes());
InputStreamReader reader = new InputStreamReader(bis,charset);
JsonReader jsonReader = mGson.newJsonReader(reader);
try {
return adapter.read(jsonReader);
} finally {
value.close();
}
}
}
4,定义异常相关类
定义 ApiErrorCode.class
Tip: SUCCUSE_CLIENT 字段必须根据实际情况修改值 如 200 等
package com.hmkj.retrofitrxjavalib.http;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:[email protected]
*/
public interface ApiErrorCode {
/** 访问成功*/
int SUCCUSE_CLIENT = 200;
/** 客户端错误*/
int ERROR_CLIENT_AUTHORIZED = 0;
/**
* 未知错误
*/
int UNKNOWN = 1000;
/**
* 解析错误
*/
int PARSE_ERROR = 1001;
/**
* 网络错误
*/
int NETWORD_ERROR = 1002;
/**
* 协议出错
*/
int HTTP_ERROR = 1003;
}
定义 ApiException.class
package com.hmkj.retrofitrxjavalib.http;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:[email protected]
*/
public class ApiException extends RuntimeException {
private int errorCode;
public String message;
public ApiException(int code, String msg) {
super(msg);
this.errorCode = code;
this.message = msg;
}
public int getErrorCode() {
return errorCode;
}
public String getMessage() {
return message;
}
}
异常处理辅助类,业务相关的公共操作都在这里完成
package com.hmkj.retrofitrxjavalib.http;
import android.content.Context;
import android.widget.Toast;
import java.io.IOException;
import retrofit2.HttpException;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:[email protected]
*/
public class ApiErrorHelper {
public static ApiException handleCommonError(Context context ,Throwable e) {
ApiException ex;
if (e instanceof HttpException) { //HTTP错误
HttpException httpException = (HttpException) e;
ex = new ApiException(ApiErrorCode.HTTP_ERROR, httpException.message());
ex.message = "网络错误"; //均视为网络错误
}else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException) {
ex = new ApiException(ApiErrorCode.PARSE_ERROR, e.getMessage());
ex.message = "解析错误"; //均视为解析错误
} else if (e instanceof ConnectException) {
ex = new ApiException(ApiErrorCode.NETWORD_ERROR,e.getMessage());
ex.message = "连接失败"; //均视为网络错误
} else if (e instanceof ApiException) { //服务器返回的错误
ex = (ApiException) e;
} else {
ex = new ApiException(ApiErrorCode.UNKNOWN,e.getMessage());
ex.message = "未知错误"; //未知错误
}
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
return ex;
}
}
5,自定义Observer 用于简化 实现方法与内部处理异常
package com.hmkj.retrofitrxjavalib.http;
import android.content.Context;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:[email protected]
*/
public abstract class BaseObserver implements Observer {
private Context mContext;
private BaseObserver() {
}
protected BaseObserver(Context context) {
mContext = context;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
ApiException apiException = ApiErrorHelper.handleCommonError(mContext, e);
onFail(apiException);
}
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(T t) {
onSuccess(t);
}
public abstract void onSuccess(T t);
public abstract void onFail(ApiException e);
}
6,数据结构基类
Tip:主要用到code 用于判断异常状态,子类继承后 不用声明基类里已有的字段,不然Gson 解析会出问题
public class BaseInfo {
//基类字段需要自己根据后台返回的JSON格式 自己定义,这里只是参考 如果字段名更改之后,
//也要同步修改自定义GsonResponseBodyConverter 里的baseInfo.get 方法
private String msg;
private int code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
7.Retrofit APIService 类
public interface ApiService {
//详情请自行百度 Retrofit ApiService 如何写
@FormUrlEncoded
@POST("/api/v3/quote/pricing/getAllPrice")
Observable getAllPoint(@FieldMap Map map);
/**
* 作用:访问网络,下载大文件。
* 默认情况下,Retrofit在处理结果前会将服务器端的Response全部读进内存。
* 如果服务器端返回的是一个非常大的文件,则容易oom。
*
* @return
*/
@Streaming
@GET
Observable getNetworkDataAsync(@Url String urlString);
}
8.添加自定义拦截器 用于添加公共参数 缓存等
public class CommonInterceptor implements Interceptor {
private String userId ;
@Override
public Response intercept(Chain chain) throws IOException {
userId = HXFinanceApplication.mApplication.getSharedPreferences("ZZD", Context.MODE_PRIVATE).getString("userId", "21");
//获取原先的请求
Request originalRequest = chain.request();
if (originalRequest.method().equals("GET")) {
//添加公共参数
HttpUrl httpUrl = originalRequest.url()
.newBuilder()
.addQueryParameter("userId", userId)
.build();
originalRequest = originalRequest.newBuilder().url(httpUrl).build();
}
if (originalRequest.method().equals("POST")) {
if (originalRequest.body() instanceof FormBody) {
FormBody.Builder bodyBuilder = new FormBody.Builder();
FormBody formBody = (FormBody) originalRequest.body();
//把原来的参数添加到新的构造器,(因为没找到直接添加,所以就new新的)
for (int i = 0; i < formBody.size(); i++) {
bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i));
}
formBody = bodyBuilder
.addEncoded("userId", userId)
.build();
originalRequest = originalRequest.newBuilder().post(formBody).build();
}
}
return chain.proceed(originalRequest);
}
}
9.代码中使用
Tip:使用的Activity/Fragment 需要先继承 RxActivity(RxAppCompatActivity)/Rxfragment(V4)
RetrofitFactory.getFactory().createService().getCenterInfo(new HashMap())
.compose(this.bindToLifecycle())//利用RxLifecycle 绑定生命周期 避免内存泄漏
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new BaseObserver(this) {
@Override
public void onSuccess(AccountCenterInfo accountCenterInfo) {
mTvContent.setText(accountCenterInfo.getData().getCurrUser().getUsername());
}
@Override
public void onFail(ApiException e) {
}
});