Commit b7e10718 by 罗翻

downloadApi修改

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