资讯专栏INFORMATION COLUMN

NoHttp封装--05 文件下载

IamDLY / 2438人阅读

摘要:下载下载按钮暂停开始等下载状态下载进度条下载地址下载请求下载地址保存的文件夹文件名是否断点续传下载如果发现文件已经存在是否删除后重新下载检查已经下载了一半的文件是什么状态文件已经下载完成下载完成已经下载完成提示用户安装代表文件不存在,需要从

xml



    

    

    

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 }

 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/3071.html

相关文章

  • NoHttp封装--04 缓存

    摘要:请求,缓存非标准协议,改变缓存模式为请求图片,缓存图片仅仅请求网络无论如何也只会请求网络,也不支持这种默认行为。仅仅读取缓存无论如何仅仅读取缓存,不会请求网络和其它操作。 1、Default模式,也是没有设置缓存模式时的默认模式 这个模式实现http协议中的内容,比如响应码是304时,当然还会结合E-Tag和LastModify等头。 StringRequest request = ne...

    Kerr1Gan 评论0 收藏0
  • NoHttp封装--02 自定义请求

    摘要:实体类请求针对的具体在中使用处理登录结果登录接口结果处理登出结果退出接口结果请求失败可以解析所有的自定义请求但是前提是传进来的必须提供了默认实现bean实体类请求: 1.bean 1 import java.io.Serializable; 2 import com.alibaba.fastjson.annotation.JSONField; 3 4 public class U...

    TigerChain 评论0 收藏0
  • NoHttp封装--06 NoHttp之队列、队列优先级

    摘要:程序入口第一种,先进先出的队列第二种,没有顺序的队列往队列中添加请求请求是一个先进先出的队列如果方法不做比较返回,那么是无序的方法是一个阻塞的方法,每次调用会拿到队列中的第一个任务,如果队列为空,这个方法将一直阻塞,知道队列中有任务再次返回public class Main { /** * 程序入口 */ public void start() { ...

    U2FsdGVkX1x 评论0 收藏0
  • NoHttp封装--01

    摘要:登陆退出处理登录结果登录接口数据处理登出结果退出接口数据请求失败添加一个请求到请求队列上下文请求对象接受回调结果,当多个请求用同一个接受结果时,用来区分请求是否显示请求是否能被用户取消是否提示用户错误信息客户端错误客户端发生错误服务器错误服NoHttpActivity 1 public class NoHttpActivity extends Activity implements Vi...

    KnewOne 评论0 收藏0
  • NoHttp封装--03 cookie

    摘要:请求自动维持支持临时的位置。支持重启关机开机后继续持久化维持。提供了接口,允许开发者监听的变化,也可以改变某个的值。服务器端登录成功登录失败客户端登录按钮成功了成功时拿到头这里就拿到了你想那的失败了文章NoHttp请求自动维持Cookie:   1.支持Session、Cookie、临时Cookie的位置。   2.支持App重启、关机开机后继续持久化维持。   3.提供了接口,允许开发者监...

    susheng 评论0 收藏0

发表评论

0条评论

IamDLY

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<