前言
Retrofit是一個RESTful的Http網(wǎng)絡(luò)請求框架的封裝翔悠。注意這里并沒有說它是網(wǎng)絡(luò)請求框架,主要原因在于網(wǎng)絡(luò)請求的工作并不是Retrofit來完成的。
Retrofit 2.0開始內(nèi)置OkHttp驾霜,前者專注于接口的封裝,后者專注于網(wǎng)絡(luò)請求的高效雾消,二者分工協(xié)作友驮。
使用步驟
1嫂沉、 添加Retrofit庫的依賴
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
2套媚、創(chuàng)建 用于描述網(wǎng)絡(luò)請求 的接口
public interface ApiService {
String HOST = "http://www.wanandroid.com";
@GET("banner/json")
Observable<BannerEntity> getBannerList();
@GET("article/list/{page}/json")
Observable<HomeEntity> getHomePageList(@Path("page") int page);
}
3、創(chuàng)建Retrofit實例
public class RetrofitHelper {
private ApiService apiService;
private static final int DEFAULT_TIMEOUT = 20;
private volatile static RetrofitHelper instance = null;
private Retrofit.Builder builder;
public static RetrofitHelper getInstance() {
if (instance == null) {
synchronized (RetrofitHelper.class) {
if (instance == null)
instance = new RetrofitHelper();
}
}
return instance;
}
private RetrofitHelper() {
builder = new Retrofit.Builder();
}
private OkHttpClient getClient() {
String PATH_DATA = BaseApplication.getInstance().getCacheDir().getAbsolutePath() + File.separator + "data";
String PATH_CACHE = PATH_DATA + "/NetCache";
File cacheFile = new File(PATH_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
builder.addNetworkInterceptor(getInterceptor());
builder.addInterceptor(getInterceptor());
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
builder.cache(cache);
//設(shè)置超時
builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
//錯誤重連
builder.retryOnConnectionFailure(true);
//cookie認證
builder.cookieJar(new CookiesManager());
return builder.build();
}
private Interceptor getInterceptor() {
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!CommonUtils.isNetworkConnected()) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
Response response = chain.proceed(request);
if (CommonUtils.isNetworkConnected()) {
int maxAge = 0;
// 有網(wǎng)絡(luò)時, 不緩存, 最大保存時長為0
response.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.removeHeader("Pragma")
.build();
} else {
// 無網(wǎng)絡(luò)時磁椒,設(shè)置超時為4周
int maxStale = 60 * 60 * 24 * 28;
response.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.removeHeader("Pragma")
.build();
}
return response;
}
};
return cacheInterceptor;
}
public ApiService getApiService(String url) {
builder.baseUrl(url)
.client(getClient())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
apiService = builder.build().create(ApiService.class);
return apiService;
}
private HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Logger.d("net log ==", message);
}
});
}
4堤瘤、發(fā)送請求
RetrofitHelper.getInstance().getApiService(Constants.ANDROID_URL).bannerList
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : BaseObserver<BannerEntity>() {
override fun onSuccess(bean: BannerEntity) {
if (bean.errorCode == 0) {
mView?.setBanner(bean)
}
}
override fun onFail(e: Throwable) {
Log.e(tag, e.message)
}
})
public abstract class BaseObserver<T> implements Observer<T> {
public abstract void onSuccess(T t);
public abstract void onFail(Throwable e);
@Override
public void onSubscribe(@NonNull Disposable d) {}
@Override
public void onNext(@NonNull T t) {
onSuccess(t);
}
@Override
public void onError(@NonNull Throwable e) {
onFail(e);
}
@Override
public void onComplete() {
}
}