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 + "%"); } }); } }
相关文章推荐
- 网络抓包工具Charles的介绍与使用
- 【翻译】rfc2616 Http状态码规范
- HttpClient发送get post请求和数据解析
- HTTP请求和响应格式
- 加载网络映射盘中的assembly失败的处理办法
- OPENCV笔记之2-如何获取网络摄像头?
- OkHttp在Android中使用详解
- 2016太原网络营销师郭文军揭秘SEM竞价中关键词分类?
- 《TCP/IP具体解释卷2:实现》笔记--IP多播
- 如何在VS2010及以上使用winpcap开发网络应用程序。
- CentOS 6 网络设置
- 加速网络访问
- 实践支持HTTPS SSL的七牛云存储CDN
- 网络编程
- iOS开发之WIFI,3G/4G两种网络同时使用技巧
- 客户端技术:Cookie 服务端技术:HttpSession
- tomact虚拟目录,虚拟主机,http请求头,相应头
- 1、Servlet 2、ServletConfig 3、ServletContext 4、HttpUrlConnection
- HttpURLConnection详解
- HttpServletResponse HttpServletRequest RequestDispatcher