我是靠谱客的博主 细心宝贝,这篇文章主要介绍利用HttpUtils实现断点续传下载文件,现在分享给大家,希望可以做个参考。


  • XUtils中包含的四大模块:

       1、DbUtils模块

       2、ViewUtils模块

       3、HttpUtils模块:

        • 支持同步,异步方式的请求;
        • 支持大文件上传,上传大文件不会oom;
        • 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT请求;
        • 下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件;
        • 返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。

       4、BitmapUtils模块

  • 这里只是运行HttpUtils模块来进行多线程下载因为该模块支持断点续传,用起来非常方便!


复制代码
1

1、开源地址  

     https://github.com/wyouflf/xUtils3.git   

    下载然后用zip解压, 取出jar包放入工程添加即可.
复制代码
1
复制代码
1
添加权限:
复制代码
1
2
3
复制代码
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

复制代码
1
Activity代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
复制代码
Boolean isDowloding = false;
HttpHandler handler;
private ProgressBar pb;
private TextView tv_error;
private TextView tv_progress;
private Button btn_down, btn_stop;
复制代码
1
复制代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
复制代码
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 初始化
    pb = (ProgressBar) findViewById(R.id.pb);
    tv_progress = (TextView) findViewById(R.id.tv_progress);
    tv_error = (TextView) findViewById(R.id.tv_failure);
    btn_down = (Button) findViewById(R.id.btn_down);
    btn_stop = (Button) findViewById(R.id.btn_stop);

    // 开始点击事件
    btn_down.setOnClickListener(this);
    // 暂停点击事件
    btn_stop.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    String fileName = "weixin_821.apk";
    switch (view.getId()) {
        case R.id.btn_down:

            btn_stop.setEnabled(true);
            btn_down.setEnabled(false);

            String path = "http://gdown.baidu.com/data/wisegame/df65a597122796a4/" + fileName;
            HttpUtils http = new HttpUtils();
            handler = http.download(path, Environment.getExternalStorageDirectory() + "/"
                    + fileName, true, true, new RequestCallBack<File>() {

                @Override
                public void onSuccess(ResponseInfo<File> arg0) {
                    isDowloding = false;
                    // 下载成功
                    Toast.makeText(MainActivity.this, arg0.result.getPath(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(HttpException arg0, String arg1) {
                    // 下载失败
                    tv_error.setText(arg1);
                }

                @Override
                public void onLoading(long total, long current, boolean isUploading) {
                    super.onLoading(total, current, isUploading);
                    if (current < total) {
                        isDowloding = true;
                    } else {
                        isDowloding = false;
                    }
                    // 下载任务
                    pb.setMax((int) total);
                    pb.setProgress((int) current);
                    tv_progress.setText(current * 100 / total + "%");
                }

            });

            break;
        case R.id.btn_stop:
            btn_stop.setEnabled(false);
            btn_down.setEnabled(true);
            if (isDowloding) {
                if (handler != null) {
                    handler.cancel();
                }
            }
            break;
    }
}

复制代码
1
复制代码
1
xml布局
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
复制代码
复制代码
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始下载" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="暂停下载" />
</LinearLayout>

<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"
    android:text="下载进度" />



最后

以上就是细心宝贝最近收集整理的关于利用HttpUtils实现断点续传下载文件的全部内容,更多相关利用HttpUtils实现断点续传下载文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部