使用第三方框架xUtils2进行下载
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用第三方框架xUtils2进行下载相关的知识,希望对你有一定的参考价值。
androidManifest.xml中添加权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.msi.xutils.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载" android:onClick="click"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_failure"/> <ProgressBar android:id="@+id/pb_download" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_progressBar"/> </LinearLayout>
MainActivity代码
package com.example.msi.xutils; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import java.io.File; public class MainActivity extends AppCompatActivity { private TextView tv_failure; private TextView tv_pargressBar; private ProgressBar pb_download; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_failure = (TextView) findViewById(R.id.tv_failure); tv_pargressBar = (TextView) findViewById(R.id.tv_progressBar); pb_download = (ProgressBar) findViewById(R.id.pb_download); } public void click(View v){ HttpUtils http = new HttpUtils(); String fileName = "TeamViewer.exe"; String webPath = "http://172.17.11.161:8080/" + fileName; http.download(webPath,//下载地址 "sdcard/TeamViewer.exe",//文件保存路径 true,//断点续传 true, //自动重命名 new RequestCallBack<File>() { @Override public void onSuccess(ResponseInfo<File> responseInfo) { Toast.makeText(MainActivity.this,responseInfo.result.getPath(),Toast.LENGTH_SHORT).show(); } @Override public void onLoading(long total, long current, boolean isUploading) { super.onLoading(total, current, isUploading); tv_pargressBar.setText(current * 100 /total + "%"); pb_download.setMax((int)total); pb_download.setProgress((int)current); } @Override public void onFailure(HttpException e, String s) { tv_failure.setText(s); } }); } }
以上是关于使用第三方框架xUtils2进行下载的主要内容,如果未能解决你的问题,请参考以下文章
Android使用xUtils3上传图片报错解决:java.lang.ArrayIndexOutOfBoundsException: 70918