zoukankan      html  css  js  c++  java
  • android82 文件下载框架xUtils

    package com.itheima.xutils;
    
    import java.io.File;
    
    import com.lidroid.xutils.HttpUtils;
    import com.lidroid.xutils.exception.HttpException;
    import com.lidroid.xutils.http.ResponseInfo;
    import com.lidroid.xutils.http.callback.RequestCallBack;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {//xUtils-2.6.14.jar
        private TextView tv_failure;
        private TextView tv_progress;
        private ProgressBar pb;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv_failure = (TextView) findViewById(R.id.tv_failure);
            tv_progress = (TextView) findViewById(R.id.tv_progress);
            pb = (ProgressBar) findViewById(R.id.pb);
        }
    
        public void click(View v){
            HttpUtils utils = new HttpUtils();//文件下载框架,支持断点续传
            String fileName = "QQPlayer.exe";
            //确定下载地址
            String path = "http://192.168.13.13:8080/" + fileName;
            utils.download(path, //下载地址
                    "sdcard/QQPlayer.exe", //文件保存路径
                    true,//是否支持断点续传
                    true, //是否支持重命名
                    new RequestCallBack<File>() {
                        //下载成功后调用
                        @Override
                        public void onSuccess(ResponseInfo<File> arg0) {
                            Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show();
                            
                        }
                        //下载失败调用
                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            // TODO Auto-generated method stub
                            tv_failure.setText(arg1);
                        }
                        @Override
                        public void onLoading(long total, long current,
                                boolean isUploading) {
                            // TODO Auto-generated method stub
                            super.onLoading(total, current, isUploading);
                            pb.setMax((int)total);
                            pb.setProgress((int)current);
                            tv_progress.setText(current * 100 / total + "%");
                        }
                    });
        }
    }
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • 相关阅读:
    Facelets应用程序的生命周期
    JavaServer Faces生命周期概述
    spring security使用自定义登录界面后,不能返回到之前的请求界面的问题
    记一次使用mybatis进行like 模糊查询遇到的问题
    用Filter作用户授权的例子
    p132代码解析
    xml
    java EE第一周博客
    spring security的简单应用
    Java包装类,以及Integer与int之间的比较
  • 原文地址:https://www.cnblogs.com/yaowen/p/4941503.html
Copyright © 2011-2022 走看看