码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之xUtils-HttpUtils的使用

时间:2015-12-22 10:12:08      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

使用xUtils框架中的网络部分HttpUtils,功能:下载,断点续传,progressbar显示进度,文本显示进度%

 1 import java.io.File;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.os.Environment;
 6 import android.view.View;
 7 import android.widget.ProgressBar;
 8 import android.widget.TextView;
 9 import android.widget.Toast;
10 
11 import com.lidroid.xutils.HttpUtils;
12 import com.lidroid.xutils.exception.HttpException;
13 import com.lidroid.xutils.http.ResponseInfo;
14 import com.lidroid.xutils.http.callback.RequestCallBack;
15 
16 public class MainActivity extends Activity {
17     private ProgressBar pb;
18     private TextView tv_error;
19     private TextView tv_progress;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         pb = (ProgressBar) findViewById(R.id.pb);
26         tv_progress = (TextView) findViewById(R.id.tv_progress);
27         tv_error = (TextView) findViewById(R.id.tv_error);
28 
29     }
30 
31     public void click(View v) {
32         String fileName = "bcompare.exe";
33         String path = "http://192.168.1.40:8080/" + fileName;
34         HttpUtils http = new HttpUtils();
35         http.download(path, Environment.getExternalStorageDirectory() + "/"
36                 + fileName, true, true, new RequestCallBack<File>() {
37 
38             @Override
39             public void onSuccess(ResponseInfo<File> arg0) {
40                 Toast.makeText(MainActivity.this, arg0.result.getPath(), 0)
41                         .show();
42             }
43 
44             @Override
45             public void onFailure(HttpException arg0, String arg1) {
46                 tv_error.setText(arg1);
47             }
48 
49             @Override
50             public void onLoading(long total, long current, boolean isUploading) {
51                 super.onLoading(total, current, isUploading);
52                 pb.setMax((int) total);
53                 pb.setProgress((int) current);
54                 tv_progress.setText(current * 100 / total + "%");
55             }
56 
57         });
58     }
59 }

 

Android开发之xUtils-HttpUtils的使用

标签:

原文地址:http://www.cnblogs.com/liyiran/p/5065699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!