Commit b7e10718 by 罗翻

downloadApi修改

parent 596e143c
package com.dayu.bigfish.api;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import com.dayu.bigfish.Constants;
import com.dayu.bigfish.base.BaseResponse;
......@@ -32,106 +30,70 @@ import retrofit2.converter.gson.GsonConverterFactory;
* Created by luofan on 2017/11/09.
*/
public class Api {
private static APIService service;
private static Retrofit retrofit;
private static final int DEFAULT_TIMEOUT = 60;
private static Retrofit downloadRetrofit;
/**
*普通retrofit.
* @return
*/
public static APIService getService() {
service = getRetrofit().create(APIService.class);
return service;
return getRetrofit().create(APIService.class);
}
public static <T> T getService(Class<T> clz) {
return getRetrofit().create(clz);
/**
* 下载的retrofit.
* @return
*/
public static APIService getDownloadService() {
return getDownloadRetrofit().create(APIService.class);
}
private static Retrofit getRetrofit() {
if (retrofit == null) {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(m -> LogUtils.i("request", m));
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
TokenInterceptord tokenInterceptord = new TokenInterceptord();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(tokenInterceptord)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
retrofit = getRetrofit("");
}
return retrofit;
}
public static APIService getDownloadRetrofit() {
private static Retrofit getDownloadRetrofit() {
if (downloadRetrofit == null) {
downloadRetrofit = getRetrofit("download");
}
return downloadRetrofit;
}
private static Retrofit getRetrofit(String type) {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(m -> Log.i("retrofit", m));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(m -> LogUtils.i("request", m));
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
TokenInterceptord tokenInterceptord = new TokenInterceptord();
OkHttpClient.Builder build = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(600, TimeUnit.SECONDS)
.writeTimeout(600, TimeUnit.SECONDS)
.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
.addInterceptor(tokenInterceptord)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
if ("download".equals(type)) {
build.addNetworkInterceptor(chain -> {
Response response = chain.proceed(chain.request());
return response
.newBuilder()
.body(new FileResponseBody(response.body()))//将自定义的ResposeBody设置给它
.build();
});
}
})
.build();
downloadRetrofit = new Retrofit.Builder()
.client(okHttpClient)
return new Retrofit.Builder()
.client(build.build())
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
return downloadRetrofit.create(APIService.class);
}
/**
* 对网络接口返回的Response进行分割操作
*
* @param response
* @param <T>
* @return
*/
public static <T> Observable<T> flatResponse(final BaseResponse<T> response) {
return Observable.create(e -> {
if (response.isSuccess()) {
if (!e.isDisposed()) {
e.onNext(response.getData());
}
} else {
if (!e.isDisposed()) {
e.onError(new ServerException(response.getSubCode(), response.getMsg()));
}
return;
}
if (!e.isDisposed()) {
e.onComplete();
}
});
}
/**
* 统一加上token.
*/
......@@ -159,6 +121,7 @@ public class Api {
return (ObservableTransformer<BaseResponse<T>, T>) transformer;
}
final static ObservableTransformer transformer = new ObservableTransformer() {
@Override
public ObservableSource apply(Observable upstream) {
......@@ -168,4 +131,31 @@ public class Api {
}
};
/**
* 对网络接口返回的Response进行分割操作
*
* @param response
* @param <T>
* @return
*/
public static <T> Observable<T> flatResponse(final BaseResponse<T> response) {
return Observable.create(e -> {
if (response.isSuccess()) {
if (!e.isDisposed()) {
e.onNext(response.getData());
}
} else {
if (!e.isDisposed()) {
e.onError(new ServerException(response.getSubCode(), response.getMsg()));
}
return;
}
if (!e.isDisposed()) {
e.onComplete();
}
});
}
}
......@@ -13,6 +13,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
......@@ -32,8 +33,8 @@ public class DownloadManager {
* @param cd 订阅关系集合,在数据传输完毕时解除订阅
*/
public static void download(final Context context, final String url, final String apkPath, final CompositeDisposable cd, onDownloadListener listener) {
Api.getDownloadRetrofit().download(url)
.map(responseBody -> responseBody.source())
Api.getDownloadService().download(url)
.map(ResponseBody::source)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.doOnNext(bufferedSource -> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment