NoHttp封装--05 文件下载

时间:2022-02-19 21:29:30

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <ProgressBar
android:id="@+id/pb_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" /> <TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btn_start_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="下载" /> </LinearLayout>

java:

 import com.yolanda.nohttp.Headers;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.download.DownloadListener;
import com.yolanda.nohttp.download.DownloadRequest;
import com.yolanda.nohttp.error.ArgumentError;
import com.yolanda.nohttp.error.ClientError;
import com.yolanda.nohttp.error.NetworkError;
import com.yolanda.nohttp.error.ServerError;
import com.yolanda.nohttp.error.StorageReadWriteError;
import com.yolanda.nohttp.error.StorageSpaceNotEnoughError;
import com.yolanda.nohttp.error.TimeoutError;
import com.yolanda.nohttp.error.URLError;
import com.yolanda.nohttp.error.UnKnownHostError;
import com.yolanda.nohttp5.R;
import com.yolanda.nohttp5.config.AppConfig;
import com.yolanda.nohttp5.nohttp.CallServer;
import com.yolanda.nohttp5.util.Toast; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView; public class DownloadActivity extends Activity implements View.OnClickListener, DownloadListener { private final static String PROGRESS_KEY = "download_progress";
/**
* 下载按钮、暂停、开始等.
*/
private TextView mBtnStart;
/**
* 下载状态.
*/
private TextView mTvResult;
/**
* 下载进度条.
*/
private ProgressBar mProgressBar;
/***
* 下载地址.
*/
private String url = "http://m.apk.67mo.com/apk/999129_21769077_1443483983292.apk";
/**
* 下载请求.
*/
private DownloadRequest downloadRequest; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download); mProgressBar = (ProgressBar) findViewById(R.id.pb_progress);
mBtnStart = (TextView) findViewById(R.id.btn_start_download);
mTvResult = (TextView) findViewById(R.id.tv_result);
mBtnStart.setOnClickListener(this); // url 下载地址
// fileFolder 保存的文件夹
// fileName 文件名
// isRange 是否断点续传下载
// isDeleteOld 如果发现文件已经存在是否删除后重新下载
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
downloadRequest = NoHttp.createDownloadRequest(url, path, "nohttp.apk", true, false); // 检查已经下载了一半的文件是什么状态
int status = downloadRequest.checkBeforeStatus();
if (status == DownloadRequest.STATUS_FINISH) {// 文件已经下载完成
mTvResult.setText("下载完成");
mBtnStart.setText("已经下载完成");
// ... 提示用户安装apk
} else if (status == DownloadRequest.STATUS_RESTART) {// 代表文件不存在,需要从头下载
mTvResult.setText("");
mBtnStart.setText("下载");
} else if (status == DownloadRequest.STATUS_RESUME) {// 代表文件已经下载了一半了
int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, );
mProgressBar.setProgress(progress);
mTvResult.setText("已经下载了 " + progress + "%");
mBtnStart.setText("继续下载");
}
} @Override
public void onClick(View v) {
if (downloadRequest.isStarted()) {
// 暂停下载
downloadRequest.cancel(true);
} else {
// what 区分下载
// downloadRequest 下载请求对象
// downloadListener 下载监听
CallServer.getDownloadInstance().add(, downloadRequest, this);
}
} /**
* @param what 代表哪一个下载
* @param isResume 是不是断点续续传开始下载的
* @param beforeLenght 断点开始的地方的文件大小
* @param headers 本地请求的时候的响应头
* @param allCount 本次需要下载多少
*/
@Override
public void onStart(int what, boolean isResume, long beforeLenght, Headers headers, long allCount) {
int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, );
mTvResult.setText("已下载: " + progress + "%");
mBtnStart.setText("暂停");
} @Override
public void onProgress(int what, int progress, long fileCount) {
AppConfig.getInstance().putInt(PROGRESS_KEY, progress);
mProgressBar.setProgress(progress);
mTvResult.setText("已经下载了 " + progress + "%");
} @Override
public void onDownloadError(int what, Exception exception) {
mBtnStart.setText("再次尝试"); String message = "下载出错了:";
if (exception instanceof ClientError) {
message += "客户端错误";
} else if (exception instanceof ServerError) {
message += "服务器发生内部错误";
} else if (exception instanceof NetworkError) {
message += "网络不可用,请检查网络";
} else if (exception instanceof StorageReadWriteError) {
message += "存储卡错误,请检查存储卡";
} else if (exception instanceof StorageSpaceNotEnoughError) {
message += "存储位置空间不足";
} else if (exception instanceof TimeoutError) {
message += "下载超时";
} else if (exception instanceof UnKnownHostError) {
message += "服务器找不到";
} else if (exception instanceof URLError) {
message += "url地址错误";
} else if (exception instanceof ArgumentError) {
message += "下载参数错误";
} else {
message += "未知错误";
}
mTvResult.setText(message);
} @Override
public void onFinish(int what, String filePath) {
Toast.show("下载完成");
mTvResult.setText("下载完成,文件保存在:" + filePath);
} @Override
public void onCancel(int what) {
mTvResult.setText("下载暂停");
mBtnStart.setText("继续下载");
} }

NoHttp封装--05 文件下载