package com.itheima.xutils;
import java.io.File; 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 android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity {//xUtils-2.6.14.jar private TextView tv_failure; private TextView tv_progress; private ProgressBar pb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_failure = (TextView) findViewById(R.id.tv_failure); tv_progress = (TextView) findViewById(R.id.tv_progress); pb = (ProgressBar) findViewById(R.id.pb); } public void click(View v){ HttpUtils utils = new HttpUtils();//文件下载框架,支持断点续传 String fileName = "QQPlayer.exe"; //确定下载地址 String path = "http://192.168.13.13:8080/" + fileName; utils.download(path, //下载地址 "sdcard/QQPlayer.exe", //文件保存路径 true,//是否支持断点续传 true, //是否支持重命名 new RequestCallBack<File>() { //下载成功后调用 @Override public void onSuccess(ResponseInfo<File> arg0) { Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show(); } //下载失败调用 @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub tv_failure.setText(arg1); } @Override public void onLoading(long total, long current, boolean isUploading) { // TODO Auto-generated method stub super.onLoading(total, current, isUploading); pb.setMax((int)total); pb.setProgress((int)current); tv_progress.setText(current * 100 / total + "%"); } }); } } <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4941503.html,如需转载请自行联系原作者