- 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>
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 }
權限:
1 <!--XUtils要添加兩個權限--> 2 <uses-permission android:name="android.permission.INTERNET"/> 3 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
運行結果: