android使用开源项目xutils3完成下载

上一次自己写了一个多线程断点续传下载的demo,过于麻烦,bug超多,所以我学习使用xutils来完成此功能。

先将xutils依赖搭建好(上一篇博客已经具体写了方法)

先看看效果图:

下面开始代码的逻辑:

在xutils3以前使用HttpUtils新建对象即可使用,3以后取消了HttpUtils,具体方法请看代码。

首先创建布局activity_main.xml:

 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:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context=".MainActivity">
 8 
 9     <EditText
10         android:id="@+id/et_path"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:hint="请输入下载路径"
14         android:text="http://10.173.29.234/gp.apk" />
15 
16     <Button
17         android:layout_width="wrap_content"
18         android:layout_height="wrap_content"
19         android:text="下载"
20         android:onClick="click"/>
21 
22     <ProgressBar
23         android:id="@+id/progressBar"
24         style="?android:attr/progressBarStyleHorizontal"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content" />
27 
28 </LinearLayout>

然后MainActivity.java:

  1 import android.app.Activity;
  2 import android.content.pm.PackageManager;
  3 import android.support.v4.app.ActivityCompat;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.view.View;
  7 import android.widget.EditText;
  8 import android.widget.ProgressBar;
  9 import android.widget.Toast;
 10 
 11 import org.xutils.common.Callback;
 12 import org.xutils.http.RequestParams;
 13 import org.xutils.x;
 14 
 15 import java.io.File;
 16 
 17 public class MainActivity extends AppCompatActivity {
 18 
 19     private EditText et_path;
 20     private ProgressBar pb;
 21     //动态获取权限
 22     private static final int REQUEST_EXTERNAL_STORAGE = 1;
 23     private static String[] PERMISSIONS_STORAGE = {
 24             "android.permission.READ_EXTERNAL_STORAGE",
 25             "android.permission.WRITE_EXTERNAL_STORAGE" };
 26     private String fileName;
 27     private String appName;
 28 
 29     @Override
 30     protected void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_main);
 33         //动态获取权限
 34         verifyStoragePermissions(this);
 35 
 36         x.view().inject(this);
 37 
 38         et_path = findViewById(R.id.et_path);
 39         pb = findViewById(R.id.progressBar);
 40 
 41 
 42     }
 43 
 44     //点击按钮下载(断点续传)
 45     public void click(View view) {
 46 
 47         //拿到路径
 48         String path = et_path.getText().toString().trim();
 49         fileName = getFileName(path);
 50         RequestParams params = new RequestParams(path);
 51         params.setSaveFilePath(fileName);
 52         params.setAutoRename(true);
 53         x.http().get(params, new Callback.ProgressCallback<File>() {
 54 
 55             @Override
 56             public void onWaiting() {
 57 
 58             }
 59 
 60             @Override
 61             public void onStarted() {
 62 
 63             }
 64 
 65             @Override
 66             public void onLoading(long total, long current, boolean isDownloading) {
 67                 //totle代表总进度,current代表当前进度
 68                 pb.setMax((int) total);
 69                 pb.setProgress((int) current);
 70                 System.out.println("测试1");
 71 
 72             }
 73 
 74 
 75             @Override
 76             public void onSuccess(File result) {
 77                 System.out.println("下载完成");
 78                 Toast.makeText(getApplicationContext(), "下载成功", Toast.LENGTH_SHORT).show();
 79 
 80             }
 81 
 82             @Override
 83             public void onError(Throwable ex, boolean isOnCallback) {
 84                 System.out.println("测试2");
 85 
 86             }
 87 
 88             @Override
 89             public void onCancelled(CancelledException cex) {
 90                 System.out.println("测试3");
 91 
 92             }
 93 
 94             @Override
 95             public void onFinished() {
 96                 System.out.println("测试4");
 97 
 98             }
 99         });
100 
101 
102     }
103     //动态获取权限
104     public static void verifyStoragePermissions(Activity activity) {
105 
106         try {
107             //检测是否有写的权限
108             int permission = ActivityCompat.checkSelfPermission(activity,
109                     "android.permission.WRITE_EXTERNAL_STORAGE");
110             if (permission != PackageManager.PERMISSION_GRANTED) {
111                 // 没有写的权限,去申请写的权限,会弹出对话框
112                 ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
113             }
114         } catch (Exception e) {
115             e.printStackTrace();
116         }
117     }
118     //获取文件名-->"http://10.173.29.234/test.exe"
119     public String getFileName(String path) {
120         int start = path.lastIndexOf("/") + 1;
121         appName = path.substring(start);
122 //        String fileName = Environment.getExternalStorageDirectory().getPath()+"/"+subString;
123         String fileName = "/sdcard/file/"+ appName;
124         return fileName;
125 
126     }
127 }

第53行注意x.http().get(params, new Callback.ProgressCallback<File>(){};<>中要写File,因为下载的是一个文件,否则文件不会下载下来。

还要加上权限:

1  <uses-permission android:name="android.permission.INTERNET" />
2     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
4     

 

posted @ 2018-11-25 20:52  龙谷情Sinoam  阅读(1393)  评论(0编辑  收藏  举报
Smiley face