您的位置:首页 > 理论基础 > 计算机网络

Android开发之xUtils-HttpUtils的使用

2015-12-22 09:59 766 查看
使用xUtils框架中的网络部分HttpUtils,功能:下载,断点续传,progressbar显示进度,文本显示进度%

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
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;

public class MainActivity extends Activity {
private ProgressBar pb;
private TextView tv_error;
private TextView tv_progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.pb);
tv_progress = (TextView) findViewById(R.id.tv_progress);
tv_error = (TextView) findViewById(R.id.tv_error);

}

public void click(View v) {
String fileName = "bcompare.exe";
String path = "http://192.168.1.40:8080/" + fileName;
HttpUtils http = new HttpUtils();
http.download(path, Environment.getExternalStorageDirectory() + "/"
+ fileName, 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) {
tv_error.setText(arg1);
}

@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
pb.setMax((int) total);
pb.setProgress((int) current);
tv_progress.setText(current * 100 / total + "%");
}

});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: