#1.下载utils项目

https://github.com/wyouflf/xUtils

#2布局文件里实现UI

<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"
tools:context=".MainActivity" > <EditText
android:id="@+id/tv_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://down.360safe.com/inst.exe" />
<Button
android:layout_marginTop="10dip"
android:layout_width="match_parent"
android:onClick="download"
android:layout_height="wrap_content"
android:text="下载"
/>
<TextView
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_marginTop="10dip"
android:layout_height="wrap_content"
android:text="提示信息"
/> </LinearLayout>

#3.在mainactivity中实现代码功能

package com.wzw.downloaddemo;

import java.io.File;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
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.EditText;
import android.widget.TextView; public class MainActivity extends Activity { private EditText etPath;
private TextView tvInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPath=(EditText) findViewById(R.id.tv_path);
tvInfo=(TextView) findViewById(R.id.tv_info); }
public void download(View v){
String path =etPath.getText().toString();
HttpUtils http = new HttpUtils();
http.download(path, "/sdcard/360.exe", true, true, new RequestCallBack<File>() { <span style="white-space:pre"> </span>@Override
public void onStart() {
tvInfo.setText("正在连接...");
} @Override
public void onLoading(long total, long current, boolean isUploading) {
tvInfo.setText(current + "/" + total);
} @Override
public void onFailure(HttpException error, String msg) {
tvInfo.setText(msg);
} @Override
public void onSuccess(ResponseInfo<File> responseInfo) {
// TODO Auto-generated method stub
tvInfo.setText("downloaded:" + responseInfo.result.getPath());
}
}); } }

用Utils的话比用传统的方式要方便的多。

Android使用开源项目Xutils实现多线程下载文件的更多相关文章

  1. 【转】近百个Android优秀开源项目

    近百个Android优秀开源项目   Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的智慧 ...

  2. Android优秀开源项目

    本文转自:http://blog.tisa7.com/android_open_source_projects Android优秀开源项目 Android经典的开源项目其实非常多,但是国内的博客总是拿 ...

  3. 网络编程之PC版与Android手机版带断点续传的多线程下载

    一.多线程下载         多线程下载就是抢占服务器资源         原理:服务器CPU 分配给每条线程的时间片相同,服务器带宽平均分配给每条线程,所以客户端开启的线程越多,就能抢占到更多的服 ...

  4. 教你如何在 Android 使用多线程下载文件

    # 教你如何在 Android 使用多线程下载文件 前言 在 Android 日常开发中,我们会经常遇到下载文件需求,这里我们也可以用系统自带的 api DownloadManager 来解决这个问题 ...

  5. java 多线程下载文件 以及URLConnection和HttpURLConnection的区别

    使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...

  6. Python之FTP多线程下载文件之分块多线程文件合并

    Python之FTP多线程下载文件之分块多线程文件合并 欢迎大家阅读Python之FTP多线程下载系列之二:Python之FTP多线程下载文件之分块多线程文件合并,本系列的第一篇:Python之FTP ...

  7. Python之FTP多线程下载文件之多线程分块下载文件

    Python之FTP多线程下载文件之多线程分块下载文件 Python中的ftplib模块用于对FTP的相关操作,常见的如下载,上传等.使用python从FTP下载较大的文件时,往往比较耗时,如何提高从 ...

  8. 多线程下载文件,ftp文件服务器

    1: 多线程下载文件 package com.li.multiplyThread; import org.apache.commons.lang3.exception.ExceptionUtils; ...

  9. java 多线程下载文件并实时计算下载百分比(断点续传)

    多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...

随机推荐

  1. SE 2014年4月18日

    实验需求:   R1 R2 R3用环回口建立IBGP对等体(使用对等体组),AS号为100                     R4 R5 R6用环回口建立IBGP对等体(使用对等体组),AS号为 ...

  2. H3C低端交换机MAC绑定

    1.MAC地址和端口的绑定<h3c>system[h3c]interface e0/1[h3c-interface]mac-address max-count #关闭交换机端口的MAC学习 ...

  3. 解决:Determining IP Information for eth0 一直停留 无法进入系统

    问题场景: vm centos6.4网卡之前一直没异常,可今天启动时一直卡在Determining IP Information for eth0,无法进入系统.网上说了非常多办法,大多都是不着边的说 ...

  4. TCP、UDP和HTTP

    先来一个讲TCP.UDP和HTTP关系的 1.TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中 ...

  5. 2012Android开发热门资料(110个)

    下载中心完整附件下载地址:http://down.51cto.com/data/412494 附件内容部分预览: 专题推荐: Android控:平板电脑HD精品游戏软件合集(共32个) http:// ...

  6. POJ 2411

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9614   Accepted: 5548 ...

  7. C# WPF Datagrid的筛选

    public static void SearchResult(DataGrid dg,string condition) { #region string code = string.Empty; ...

  8. Java EE (12) -- 系统质量的分类

    明显的 性能(Performance): 对响应用户的应答时间的度量.可靠性(Reliability): 对包括后台存储和给用户的表示结果在内的数据正确的可能性的度量.可用性(Availability ...

  9. UNIX网络编程卷1 server程序设计范式7 预先创建线程,以相互排斥锁上锁方式保护accept

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.预先创建一个线程池.并让每一个线程各自调用 accept 2.用相互排斥锁代替让每一个线 ...

  10. ASF (0) - ASF Java 项目总览

    Apache .NET Ant Library This is a library of Ant tasks that help developing .NET software. It includ ...