Andriod文件下载含服务器端(客户端 UI界面异步请求部分)三

本文采用AsyncTask来实现异步请求,具体代码如下:

public class downloadActivity extends Activity {

	private TextView myTextView=null;
	private Button button=null;
	private static final String path="http://192.168.0.179:8080/Myweb/download.do";
	private ProgressDialog progressDialog=null;
	private URL url=null;
   @Override
   protected void onCreate(Bundle savedInstanceState) {

	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.download);

	  //获取传过来的用户名
	  Intent intent = getIntent();
	  String value = intent.getStringExtra("username");
	  value="hello"+" "+value;
	   //
	  myTextView = (TextView) findViewById(R.id.textView1);
	  myTextView.setText(value);
	  button=(Button)this.findViewById(R.id.download_btn);
	  progressDialog=new ProgressDialog(this);
	  progressDialog.setCancelable(false);
	  progressDialog.setTitle("提示");
	  progressDialog.setMessage("请耐心等待,文件正在下载中....");
	  try {
		url=new URL(path);
	  } catch (MalformedURLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	  }
	  button.setOnClickListener(new OnClickListener() {

		public void onClick(View arg0) {
			//progressDialog.show();
			 new DownloadFilesTask().execute(url);

		}
	});

    }

   private class DownloadFilesTask extends AsyncTask<URL, Void, Void> {

	@Override
	protected void onPreExecute() {
		progressDialog.show();
		super.onPreExecute();
	}
	@Override
	protected Void doInBackground(URL... urls) {
		httpUtils.sendDownloadPost(urls[0]);
		return null;
	} 

	@Override
	protected void onPostExecute(Void result) {
		progressDialog.dismiss();
		super.onPostExecute(result);
	}
   }

}

在注册时,使用Intent传递了username数据。

AsyncTask<URL, Void, Void>

主要有三个参数

  1. Params, the type of the parameters sent to the task upon execution.  本文传递URL数据
  2. Progress, the type of the progress units published during the background computation.//进度条
  3. Result, the type of the result of the background computation.  //结果

本文未采用进度条形式,只使用了ProgressDialog,故第二个参数置为空,第三个参数选取时,本文在httpUtils包类已经写入文件到sd卡,故也置为空。

重写的方法:

  1. onPreExecute(),
    invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.  UI主线程中,初始化参数
  2. doInBackground(Params...),
    invoked on the background thread immediately after onPreExecute() finishes
    executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step.
    This step can also use publishProgress(Progress...) to
    publish one or more units of progress. These values are published on the UI thread, in theonProgressUpdate(Progress...) step.
     非主线程,耗时操作
  3. onProgressUpdate(Progress...),
    invoked on the UI thread after a call to publishProgress(Progress...).
    The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
  4. onPostExecute(Result),
    invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.  结果更新
时间: 2024-07-31 04:21:42

Andriod文件下载含服务器端(客户端 UI界面异步请求部分)三的相关文章

Andriod文件下载含服务器端(工具包 Httputils)二

同样在上一篇服务器的基础上,本文客户端也在登录界面 Andriod简单http get请求基础上,用户注册后跳转到下载界面,本文下载界面只有两个View,一个是textView显示注册后用户名(本文未做登录界面,方法与注册类似,只是在服务器端查询数据库中用户名,密码是否正确).另一个为下载按钮,点击后下载到sd卡中. 下面先将工具包,该类封装了Http请求,本文使用get方法,使用HttpURLConnection类来负责具体请求. httpUtils类中添加sendDownloadPost方法

Andriod文件下载含服务器端(服务器端)一

本文继续讲解andriod网络方面问题.本文主要在Eclipse上搭建服务器端. 同上篇,本文tomcat版本7.0,servlet 3.0不需要在web.xml下注册xml文件. 本文实现的内容较简单,在服务器在磁盘上读取一个pdf(其他文件也行),返回一个流文件给客户端. 服务器端的代码如下: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExcept

Android含文档server结束(client UI接口异步请求的一部分)三

在本文中,AsyncTask为了实现异步请求,详细代码如下所示的: public class downloadActivity extends Activity { private TextView myTextView=null; private Button button=null; private static final String path="http://192.168.0.179:8080/Myweb/download.do"; private ProgressDial

【转】关于“不同浏览器的并发异步请求”的简单研究

之前在开发过程中有发现在一个页面内同时对单个url发出多个Ajax请求最后完成回调的只有最后一个.具体是什么原因导致的呢?针对这个问题,本人做了进一步的测试. 对于单个页面内并发的异步请求分为以下几种情况: 并发多个相同url的请求 并发多个不同url的请求(参数不同,协议相同) 并发多个不同url的请求(参数和协议都不同) 测试方法: 客户端:同时发出N异步请求,并输出发出请求的时间:当服务器返回相应数据后将接受请求的时间输出,并输出之前所发送的请求的标识ID. 服务器:服务器接到请求后延时一

Android异步处理二:使用AsyncTask异步更新UI界面

Android异步处理二:使用AsyncTask异步更新UI界面 - lzc的专栏 - 博客频道 - CSDN.NET 在<Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面>中,我们使用Thread+Handler的方式实现了异步更新UI界面,这一篇中,我们介绍一种更为简洁的实现方式:使用AsyncTask异步更新UI界面. 概述: AsyncTask是在Android SDK 1.5之后推出的一个方便编写后台线程与UI线程交互的辅助类.AsyncTask的

Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面

Android应用的开发过程中需要把繁重的任务(IO,网络连接等)放到其他线程中异步执行,达到不阻塞UI的效果. 下面将由浅入深介绍Android进行异步处理的实现方法和系统底层的实现原理. 本文介绍Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面: 即如何使用Thread+Handler的方式从非UI线程发送界面更新消息到UI线程. 概述:每个Android应用程序都运行在一个dalvik虚拟机进程中,进程开始的时候会启动一个主线程(MainThread),

android异步更新UI界面的方法

在android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处理,然后更新用户界面显示.但是,在主线线程之外的线程中直接更新页面显示的问题是:系统会报这个异常,android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view hierarchy can touch its views. (只有原始创建这个视图层次(view hierach

Android异步处理系列文章四篇之一使用Thread+Handler实现非UI线程更新UI界面

目录: Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面Android异步处理二:使用AsyncTask异步更新UI界面Android异步处理三:Handler+Looper+MessageQueue深入详解Android异步处理四:AsyncTask的实现原理 Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面 概述:每个Android应用程序都运行在一个dalvik虚拟机进程中,进程开始的时候会启动一个主线程(MainTh

Android异步处理二:AsynTask介绍和使用AsyncTask异步更新UI界面

在上一篇(http://blog.csdn.net/xlgen157387/article/details/45269389)中介绍了使用Thread+Handler实现非UI线程更新UI界面的方法步骤,下边做一下如何同构AsyncTask异步任务来更新UI界面. (1)AsyncTask的介绍 通过上图中的AsyncTask的源码结构图可以看到,主要用于重载的方法是doInBackground(),onPreExecute().onPostExecute().onProgressUpdate(