181 lines
6.0 KiB
Java
181 lines
6.0 KiB
Java
|
|
package com.migu.libnetwork;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.os.Environment;
|
||
|
|
|
||
|
|
import androidx.annotation.NonNull;
|
||
|
|
|
||
|
|
|
||
|
|
import com.pukka.libcore.utils.ContextUtils;
|
||
|
|
import com.pukka.libcore.utils.LogUtils;
|
||
|
|
|
||
|
|
import java.io.File;
|
||
|
|
import java.io.FileOutputStream;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.io.InputStream;
|
||
|
|
|
||
|
|
import io.reactivex.rxjava3.core.Observer;
|
||
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
||
|
|
import okhttp3.ResponseBody;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* content: 处理接口返回的请求结果
|
||
|
|
*/
|
||
|
|
public class RxJavaFileObserver implements Observer<ResponseBody> {
|
||
|
|
|
||
|
|
private final HttpRequestFileCallback mFileCallBack;
|
||
|
|
private static final String TAG = "RxJavaFileObserver";
|
||
|
|
private final int mType;
|
||
|
|
private final Context mContext;
|
||
|
|
private Disposable disposable;
|
||
|
|
private String destDir;
|
||
|
|
//private String fileName = "pukkaweb.apk";
|
||
|
|
private String fileName = "pukkaweb"+System.currentTimeMillis()+".apk";
|
||
|
|
RxJavaFileObserver(int type, HttpRequestFileCallback mFileCallBack) {
|
||
|
|
this.mFileCallBack = mFileCallBack;
|
||
|
|
this.mContext = ContextUtils.getContext();
|
||
|
|
this.mType = type;
|
||
|
|
if (null != mContext) {
|
||
|
|
if (mContext.getExternalCacheDir() != null) {
|
||
|
|
destDir = mContext.getExternalCacheDir() + "/jiecheng/";
|
||
|
|
} else if(null != mContext.getExternalFilesDir("")) {
|
||
|
|
destDir = mContext.getExternalFilesDir("") + "/jiecheng/";
|
||
|
|
}else{
|
||
|
|
destDir = Environment.getExternalStorageDirectory() + "/jiecheng/";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
LogUtils.d(TAG, "destDir mContext:" + mContext);
|
||
|
|
LogUtils.d(TAG, "destDir:" + destDir);
|
||
|
|
LogUtils.d(TAG, "destDir getFilesDir:" + mContext.getFilesDir());
|
||
|
|
LogUtils.d(TAG, "destDir getCacheDir:" + mContext.getCacheDir());
|
||
|
|
LogUtils.d(TAG, "destDir getExternalFilesDir:" + mContext.getExternalFilesDir(""));
|
||
|
|
LogUtils.d(TAG, "destDir getExternalCacheDir:" + mContext.getExternalCacheDir());
|
||
|
|
}
|
||
|
|
|
||
|
|
RxJavaFileObserver(Context context, int type, String fileName, HttpRequestFileCallback mFileCallBack) {
|
||
|
|
this.mFileCallBack = mFileCallBack;
|
||
|
|
this.mContext = context;
|
||
|
|
this.mType = type;
|
||
|
|
if (mContext.getExternalCacheDir() != null) {
|
||
|
|
destDir = mContext.getExternalCacheDir() + "/jiecheng/";
|
||
|
|
} else {
|
||
|
|
destDir = mContext.getExternalFilesDir("") + "/jiecheng/";
|
||
|
|
}
|
||
|
|
LogUtils.d(TAG, "destDir mContext:" + mContext);
|
||
|
|
LogUtils.d(TAG, "destDir:" + destDir);
|
||
|
|
LogUtils.d(TAG, "destDir getFilesDir:" + mContext.getFilesDir());
|
||
|
|
LogUtils.d(TAG, "destDir getCacheDir:" + mContext.getCacheDir());
|
||
|
|
LogUtils.d(TAG, "destDir getExternalFilesDir:" + mContext.getExternalFilesDir(""));
|
||
|
|
LogUtils.d(TAG, "destDir getExternalCacheDir:" + mContext.getExternalCacheDir());
|
||
|
|
|
||
|
|
this.fileName = fileName;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onSubscribe(@NonNull Disposable d) {
|
||
|
|
disposable = d;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Disposable getDisposable() {
|
||
|
|
return disposable;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 接口请求成功
|
||
|
|
*/
|
||
|
|
@Override
|
||
|
|
public void onNext(@NonNull ResponseBody responseBody) {
|
||
|
|
LogUtils.d(TAG, "responseBody.contentLength()" + responseBody.contentLength());
|
||
|
|
try {
|
||
|
|
saveFile(responseBody, destDir, fileName);
|
||
|
|
} catch (IOException e) {
|
||
|
|
LogUtils.d(TAG, "onError onNext");
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 接口请求失败
|
||
|
|
*/
|
||
|
|
@Override
|
||
|
|
public void onError(@NonNull Throwable e) {
|
||
|
|
LogUtils.d(TAG, "onError");
|
||
|
|
// 1.检查网络设置
|
||
|
|
if (!HttpTool.hasNetwork()) {
|
||
|
|
onComplete();
|
||
|
|
mFileCallBack.onRequestNetFail(mType);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// 2.非网络错误,接口请求错误
|
||
|
|
mFileCallBack.onRequestFail(e.getMessage(), "0000", mType);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 接口请求完成
|
||
|
|
*/
|
||
|
|
@Override
|
||
|
|
public void onComplete() {
|
||
|
|
LogUtils.d(TAG, "onComplete");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 将文件写入本地
|
||
|
|
*
|
||
|
|
* @param responseBody 请求结果全体
|
||
|
|
* @param destFileDir 目标文件夹
|
||
|
|
* @param destFileName 目标文件名
|
||
|
|
* @return 写入完成的文件
|
||
|
|
* @throws IOException IO异常
|
||
|
|
*/
|
||
|
|
private void saveFile(ResponseBody responseBody, String destFileDir, String destFileName) throws IOException {
|
||
|
|
InputStream is = null;
|
||
|
|
byte[] buf = new byte[2048];
|
||
|
|
int len;
|
||
|
|
FileOutputStream fos = null;
|
||
|
|
try {
|
||
|
|
is = responseBody.byteStream();
|
||
|
|
final long total = responseBody.contentLength();
|
||
|
|
long sum = 0;
|
||
|
|
|
||
|
|
File dir = new File(destFileDir);
|
||
|
|
if (!dir.exists()) {
|
||
|
|
dir.mkdirs();
|
||
|
|
}
|
||
|
|
mFileCallBack.onStart();
|
||
|
|
File file = new File(dir, destFileName);
|
||
|
|
LogUtils.d(TAG, "filePath:" + file.getPath());
|
||
|
|
fos = new FileOutputStream(file);
|
||
|
|
int progress = 0;
|
||
|
|
while ((len = is.read(buf)) != -1) {
|
||
|
|
sum += len;
|
||
|
|
fos.write(buf, 0, len);
|
||
|
|
final long finalSum = sum;
|
||
|
|
if (progress != (int) (finalSum * 100 / total)) {
|
||
|
|
progress = (int) (finalSum * 100 / total);
|
||
|
|
LogUtils.d(TAG, "responseLength onProgress" + progress + "...");
|
||
|
|
mFileCallBack.onProgress((int) progress);
|
||
|
|
if (progress == 100) {
|
||
|
|
// 下载完成
|
||
|
|
mFileCallBack.onComplete(file);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
fos.flush();
|
||
|
|
}catch (Exception e){
|
||
|
|
LogUtils.d(TAG, "onError saveFile");
|
||
|
|
mFileCallBack.downFail();
|
||
|
|
e.printStackTrace();
|
||
|
|
}finally {
|
||
|
|
try {
|
||
|
|
if (is != null) {
|
||
|
|
is.close();
|
||
|
|
}
|
||
|
|
if (fos != null) {
|
||
|
|
fos.close();
|
||
|
|
}
|
||
|
|
} catch (IOException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|