NoHttp封装--05 文件下载
程序员文章站
2022-06-05 08:14:38
xml java: ......
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:
1 import com.yolanda.nohttp.Headers; 2 import com.yolanda.nohttp.NoHttp; 3 import com.yolanda.nohttp.download.DownloadListener; 4 import com.yolanda.nohttp.download.DownloadRequest; 5 import com.yolanda.nohttp.error.ArgumentError; 6 import com.yolanda.nohttp.error.ClientError; 7 import com.yolanda.nohttp.error.NetworkError; 8 import com.yolanda.nohttp.error.ServerError; 9 import com.yolanda.nohttp.error.StorageReadWriteError; 10 import com.yolanda.nohttp.error.StorageSpaceNotEnoughError; 11 import com.yolanda.nohttp.error.TimeoutError; 12 import com.yolanda.nohttp.error.URLError; 13 import com.yolanda.nohttp.error.UnKnownHostError; 14 import com.yolanda.nohttp5.R; 15 import com.yolanda.nohttp5.config.AppConfig; 16 import com.yolanda.nohttp5.nohttp.CallServer; 17 import com.yolanda.nohttp5.util.Toast; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.Environment; 22 import android.view.View; 23 import android.widget.ProgressBar; 24 import android.widget.TextView; 25 26 public class DownloadActivity extends Activity implements View.OnClickListener, DownloadListener { 27 28 private final static String PROGRESS_KEY = "download_progress"; 29 /** 30 * 下载按钮、暂停、开始等. 31 */ 32 private TextView mBtnStart; 33 /** 34 * 下载状态. 35 */ 36 private TextView mTvResult; 37 /** 38 * 下载进度条. 39 */ 40 private ProgressBar mProgressBar; 41 /*** 42 * 下载地址. 43 */ 44 private String url = "http://m.apk.67mo.com/apk/999129_21769077_1443483983292.apk"; 45 /** 46 * 下载请求. 47 */ 48 private DownloadRequest downloadRequest; 49 50 @Override 51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 setContentView(R.layout.activity_download); 54 55 mProgressBar = (ProgressBar) findViewById(R.id.pb_progress); 56 mBtnStart = (TextView) findViewById(R.id.btn_start_download); 57 mTvResult = (TextView) findViewById(R.id.tv_result); 58 mBtnStart.setOnClickListener(this); 59 60 // url 下载地址 61 // fileFolder 保存的文件夹 62 // fileName 文件名 63 // isRange 是否断点续传下载 64 // isDeleteOld 如果发现文件已经存在是否删除后重新下载 65 String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 66 downloadRequest = NoHttp.createDownloadRequest(url, path, "nohttp.apk", true, false); 67 68 // 检查已经下载了一半的文件是什么状态 69 int status = downloadRequest.checkBeforeStatus(); 70 if (status == DownloadRequest.STATUS_FINISH) {// 文件已经下载完成 71 mTvResult.setText("下载完成"); 72 mBtnStart.setText("已经下载完成"); 73 // ... 提示用户安装apk 74 } else if (status == DownloadRequest.STATUS_RESTART) {// 代表文件不存在,需要从头下载 75 mTvResult.setText(""); 76 mBtnStart.setText("下载"); 77 } else if (status == DownloadRequest.STATUS_RESUME) {// 代表文件已经下载了一半了 78 int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, 0); 79 mProgressBar.setProgress(progress); 80 mTvResult.setText("已经下载了 " + progress + "%"); 81 mBtnStart.setText("继续下载"); 82 } 83 } 84 85 @Override 86 public void onClick(View v) { 87 if (downloadRequest.isStarted()) { 88 // 暂停下载 89 downloadRequest.cancel(true); 90 } else { 91 // what 区分下载 92 // downloadRequest 下载请求对象 93 // downloadListener 下载监听 94 CallServer.getDownloadInstance().add(0, downloadRequest, this); 95 } 96 } 97 98 /** 99 * @param what 代表哪一个下载 100 * @param isResume 是不是断点续续传开始下载的 101 * @param beforeLenght 断点开始的地方的文件大小 102 * @param headers 本地请求的时候的响应头 103 * @param allCount 本次需要下载多少 104 */ 105 @Override 106 public void onStart(int what, boolean isResume, long beforeLenght, Headers headers, long allCount) { 107 int progress = AppConfig.getInstance().getInt(PROGRESS_KEY, 0); 108 mTvResult.setText("已下载: " + progress + "%"); 109 mBtnStart.setText("暂停"); 110 } 111 112 @Override 113 public void onProgress(int what, int progress, long fileCount) { 114 AppConfig.getInstance().putInt(PROGRESS_KEY, progress); 115 mProgressBar.setProgress(progress); 116 mTvResult.setText("已经下载了 " + progress + "%"); 117 } 118 119 @Override 120 public void onDownloadError(int what, Exception exception) { 121 mBtnStart.setText("再次尝试"); 122 123 String message = "下载出错了:"; 124 if (exception instanceof ClientError) { 125 message += "客户端错误"; 126 } else if (exception instanceof ServerError) { 127 message += "服务器发生内部错误"; 128 } else if (exception instanceof NetworkError) { 129 message += "网络不可用,请检查网络"; 130 } else if (exception instanceof StorageReadWriteError) { 131 message += "存储卡错误,请检查存储卡"; 132 } else if (exception instanceof StorageSpaceNotEnoughError) { 133 message += "存储位置空间不足"; 134 } else if (exception instanceof TimeoutError) { 135 message += "下载超时"; 136 } else if (exception instanceof UnKnownHostError) { 137 message += "服务器找不到"; 138 } else if (exception instanceof URLError) { 139 message += "url地址错误"; 140 } else if (exception instanceof ArgumentError) { 141 message += "下载参数错误"; 142 } else { 143 message += "未知错误"; 144 } 145 mTvResult.setText(message); 146 } 147 148 @Override 149 public void onFinish(int what, String filePath) { 150 Toast.show("下载完成"); 151 mTvResult.setText("下载完成,文件保存在:" + filePath); 152 } 153 154 @Override 155 public void onCancel(int what) { 156 mTvResult.setText("下载暂停"); 157 mBtnStart.setText("继续下载"); 158 } 159 160 }