您的位置:首页 > 其它

安卓系统下的多线程断点下载实现2利用开源框架XUtils

2015-03-17 13:02 369 查看
使用开源框架可以大大降低开发的难度,减少开发的周期,并且bug也少的多,软件运行起来更稳定。

xUtils简介

xUtils 包含了很多实用的android工具。

xUtils 支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响…

xUitls 最低兼容android 2.2 (api level 8)

下载地址:https://github.com/wyouflf/xUtils

下面是一个demo:

public class MainActivity extends Activity {
    private EditText et_path;
    private TextView tv_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_path = (EditText) findViewById(R.id.et_path);
        tv_info = (TextView) findViewById(R.id.tv_info);
    }

    public void download(View view){
        String path = et_path.getText().toString().trim();
        if(TextUtils.isEmpty(path)){
            Toast.makeText(this, "请输入下载的路径", 0).show();
            return;
        }else{
            HttpUtils http = new HttpUtils();
            HttpHandler handler = http.download(path,
                    "/sdcard/xxx.zip",
                    true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
                    true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
                    new RequestCallBack<File>() {

                        @Override
                        public void onStart() {
                            tv_info.setText("conn...");
                        }

                        @Override
                        public void onLoading(long total, long current, boolean isUploading) {
                            tv_info.setText(current + "/" + total);
                        }

                        @Override
                        public void onSuccess(ResponseInfo<File> responseInfo) {
                            tv_info.setText("downloaded:" + responseInfo.result.getPath());
                        }
                        @Override
                        public void onFailure(HttpException error, String msg) {
                            tv_info.setText(msg);
                        }
                });
        }
    }
}


利用现成的开源框架,实现起来好处多多!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: