XUtils开源框架的使用(HttpUtils支持多线程断点续传)

  •  XUtils项目下载地址:https://github.com/wyouflf/xUtils
  • 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 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="com.ahu.lichang.httputils_multithreaddownload.MainActivity"
12     android:orientation="vertical">
13     <Button
14         android:text="HttpUtils多线程下载"
15         android:onClick="download"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content" />
18     <TextView
19         android:id="@+id/tv_failure"
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:hint="下载失败提示" />
23     <ProgressBar
24         android:id="@+id/pb"
25         style="@android:style/Widget.ProgressBar.Horizontal"
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content" />
28     <TextView
29         android:id="@+id/tv_progress"
30         android:text="下载进度"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content" />
33 </LinearLayout>
View Code

 

  MainActivity:

 1 package com.ahu.lichang.httputils_multithreaddownload;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.ProgressBar;
 7 import android.widget.TextView;
 8 import android.widget.Toast;
 9 
10 import com.lidroid.xutils.HttpUtils;
11 import com.lidroid.xutils.exception.HttpException;
12 import com.lidroid.xutils.http.ResponseInfo;
13 import com.lidroid.xutils.http.callback.RequestCallBack;
14 
15 import java.io.File;
16 
17 public class MainActivity extends Activity {
18     private TextView tv_failure;
19     private TextView tv_progress;
20     private ProgressBar pb;
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         tv_failure = (TextView) findViewById(R.id.tv_failure);
26         tv_progress = (TextView) findViewById(R.id.tv_progress);
27         pb = (ProgressBar) findViewById(R.id.pb);
28     }
29 
30     public void download(View view){
31         String path = "http://172.23.13.179:8080/QQPlayer.exe";
32         HttpUtils httpUtils = new HttpUtils();
33         httpUtils.download(path,//下载地址
34                 "storage/sdcard/QQPlayer.exe",//下载的数据保存的路径和文件名
35                 true,//是否开启断点续传
36                 true,//如果服务器响应头中包含了文件名,那么下载完毕后自动重命名
37                 new RequestCallBack<File>() {//侦听下载状态
38                     @Override
39                     public void onSuccess(ResponseInfo<File> responseInfo) {
40                         Toast.makeText(MainActivity.this,responseInfo.result.getPath(),Toast.LENGTH_SHORT).show();
41                     }
42 
43                     @Override
44                     public void onFailure(HttpException e, String s) {
45                         tv_failure.setText(s);
46                     }
47 
48                     @Override
49                     public void onLoading(long total, long current, boolean isUploading) {
50                         super.onLoading(total, current, isUploading);
51                         pb.setMax((int) total);
52                         pb.setProgress((int) current);
53                         tv_progress.setText(current * 100 / total + "%");
54                     }
55                 }
56         );
57     }
58 }
View Code

  权限:

1     <!--XUtils要添加两个权限-->
2     <uses-permission android:name="android.permission.INTERNET"/>
3     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  运行结果:

 

 

posted @ 2017-03-22 21:48  ahu-lichang  阅读(2329)  评论(0编辑  收藏  举报