xutils的来实现断点续传

1.引入xutils的jar包 xUtils-2.6.14.jar2.如果是android 6.0还需要添加aphace httpclient的jar包 org.apache.http.legacy.jar

public class MainActivity extends Activity {

    private TextView tv;
    private ProgressBar pb;
    private TextView tvProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv_failure);

        pb = (ProgressBar) findViewById(R.id.pb);

        tvProgress = (TextView) findViewById(R.id.tv_progress);
    }

    public void startDown(View v) {
        HttpUtils http = new HttpUtils();
        String fileName = "pdf.exe";
        String path = "http://192.168.1.103:8080/android/" + fileName;

        HttpHandler handler = http.download(path, // 下载地址
                Environment.getExternalStorageDirectory() + File.separator + fileName, // 保存路径
                true, // 是否支持断点续传
                true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
                new RequestCallBack<File>() {

                    @Override
                    public void onLoading(long total, long current,
                            boolean isUploading) {
                        super.onLoading(total, current, isUploading);
                        pb.setMax((int) total);
                        pb.setProgress((int) current);
                        tvProgress.setText((current * 100 / total ) + "%");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<File> responseInfo) {
                        File file = (File) responseInfo.result;
                        Toast.makeText(MainActivity.this, file.getPath(), 0)
                                .show();
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        tv.setText(msg);
                    }
                });
    }
}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.multidown.MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startDown"
        android:text="开始下载" />

    <TextView
        android:id="@+id/tv_failure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/pb"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>  

效果图:
[图片上传中。。。(1)]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,322评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,384评论 19 139
  • 教育家爱尔维修说过:人刚生下来都一样,仅仅由于环境和教育的不同,有人可能成为天才,有人则变成凡夫俗子甚至蠢材。即使...
    上善若水芳阅读 5,435评论 5 6
  • 拆书训练营09:捕捉的技巧 出自:《搞定1》(P41-43) 目标:掌握捕捉收集任务的技巧 R阅读原文 百分之百的...
    胖胖芳芳阅读 968评论 0 0
  • 写给我未来的老婆啊娇!从别人异样的目光中发现我选择她是癞蛤蟆吃上了天鹅肉,而她选择我,从属她脑子进水了!这种感受让...
    lv东子阅读 793评论 0 0