前言
一直以來想自己封裝一套合適的聯(lián)網(wǎng)請求框架,所以查詢各種資料踩過各種坑之后鼓搗出這一套框架,貼出來希望大家多多指點,共同進步
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,網(wǎng)絡(luò)請求部分
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 {
//服務(wù)器主機地址
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 重寫三個相關(guān)類
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:591079255@qq.com
*/
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<ResponseBody, ?> 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:591079255@qq.com
*/
public class MyGsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
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<T> adapter;
public MyGsonRequestBodyConverter(Gson gson, TypeAdapter<T> 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:591079255@qq.com
*/
public class MyGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private final Gson mGson;
private final TypeAdapter<T> adapter;
public MyGsonResponseBodyConverter(Gson gson, TypeAdapter<T> 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);
//關(guān)注的重點,自定義響應(yīng)碼中非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,定義異常相關(guān)類
定義 ApiErrorCode.class
Tip: SUCCUSE_CLIENT 字段必須根據(jù)實際情況修改值 如 200 等
package com.hmkj.retrofitrxjavalib.http;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:591079255@qq.com
*/
public interface ApiErrorCode {
/** 訪問成功*/
int SUCCUSE_CLIENT = 200;
/** 客戶端錯誤*/
int ERROR_CLIENT_AUTHORIZED = 0;
/**
* 未知錯誤
*/
int UNKNOWN = 1000;
/**
* 解析錯誤
*/
int PARSE_ERROR = 1001;
/**
* 網(wǎng)絡(luò)錯誤
*/
int NETWORD_ERROR = 1002;
/**
* 協(xié)議出錯
*/
int HTTP_ERROR = 1003;
}
定義 ApiException.class
package com.hmkj.retrofitrxjavalib.http;
/**
* Created by zhuzidong
* on 2018/1/30.
* Email:591079255@qq.com
*/
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;
}
}
異常處理輔助類,業(yè)務(wù)相關(guān)的公共操作都在這里完成
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:591079255@qq.com
*/
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 = "網(wǎng)絡(luò)錯誤"; //均視為網(wǎng)絡(luò)錯誤
}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 = "連接失敗"; //均視為網(wǎng)絡(luò)錯誤
} else if (e instanceof ApiException) { //服務(wù)器返回的錯誤
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 用于簡化 實現(xiàn)方法與內(nèi)部處理異常
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:591079255@qq.com
*/
public abstract class BaseObserver<T> implements Observer<T> {
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,數(shù)據(jù)結(jié)構(gòu)基類
Tip:主要用到code 用于判斷異常狀態(tài),子類繼承后 不用聲明基類里已有的字段,不然Gson 解析會出問題
public class BaseInfo {
//基類字段需要自己根據(jù)后臺返回的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<AllPriceInfo> getAllPoint(@FieldMap Map<String, String> map);
/**
* 作用:訪問網(wǎng)絡(luò),下載大文件。
* 默認情況下,Retrofit在處理結(jié)果前會將服務(wù)器端的Response全部讀進內(nèi)存。
* 如果服務(wù)器端返回的是一個非常大的文件,則容易oom。
*
* @return
*/
@Streaming
@GET
Observable<ResponseBody> getNetworkDataAsync(@Url String urlString);
}
8.添加自定義攔截器 用于添加公共參數(shù) 緩存等
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")) {
//添加公共參數(shù)
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();
//把原來的參數(shù)添加到新的構(gòu)造器,(因為沒找到直接添加,所以就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<String, String>())
.compose(this.<AccountCenterInfo>bindToLifecycle())//利用RxLifecycle 綁定生命周期 避免內(nèi)存泄漏
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new BaseObserver<AccountCenterInfo>(this) {
@Override
public void onSuccess(AccountCenterInfo accountCenterInfo) {
mTvContent.setText(accountCenterInfo.getData().getCurrUser().getUsername());
}
@Override
public void onFail(ApiException e) {
}
});