- 浏览: 237126 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
江恂泽:
楼主 这家伙会把data 拆分开来 转换成day year形 ...
JsonUtil -
mdsp25xhm:
为何没有列表查询功能?
myBatis DAO封装
AsyncTask是抽象类,子类必须实现抽象方法doInBackground(Params... p) ,在此方法中实现任务的执行工作,比如连接网络获取数据等。通常还应该实现onPostExecute(Result r)方法,因为应用程序关心的结果在此方法中返回。需要注意的是AsyncTask一定要在主线程中创建实例。AsyncTask定义了三种泛型类型 Params,Progress和Result。
* Params 启动任务执行的输入参数,比如HTTP请求的URL。
* Progress 后台任务执行的百分比。
* Result 后台执行任务最终返回的结果,比如String。
AsyncTask 的执行分为四个步骤,与前面定义的TaskListener类似。每一步都对应一个回调方法,需要注意的是这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法。在任务的执行过程中,这些方法被自动调用。
* onPreExecute() 当任务执行之前开始调用此方法,可以在这里显示进度对话框。
* doInBackground(Params...) 此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间。在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。
* onProgressUpdate(Progress...) 此方法在主线程执行,用于显示任务执行的进度。
* onPostExecute(Result) 此方法在主线程执行,任务执行的结果作为此方法的参数返回。
PageTask扩展了AsyncTask,在 doInBackground()方法中读取网页内容。PageTask的源代码如下所示:
// 设置三种类型参数分别为String,Integer,String
class PageTask extends AsyncTask<String, Integer, String> {
// 可变长的输入参数,与AsyncTask.exucute()对应
@Override
protected String doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
// params[0] 代表连接的url
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
String s = null;
if (is != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[128];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
baos.write(buf, 0, ch);
count += ch;
if (length > 0) {
// 如果知道响应的长度,调用publishProgress()更新进度
publishProgress((int) ((count / (float) length) * 100));
}
// 为了在模拟器中清楚地看到进度,让线程休眠100ms
Thread.sleep(100);
}
s = new String(baos.toByteArray()); }
// 返回结果
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
// 返回HTML页面的内容
message.setText(result);
}
@Override
protected void onPreExecute() {
// 任务启动,可以在这里显示一个对话框,这里简单处理
message.setText(R.string.task_started);
}
@Override
protected void onProgressUpdate(Integer... values) {
// 更新进度
message.setText(values[0]);
}
}
复制代码
执行PageTask非常简单,只需要调用如下代码。重新运行NetworkActivity,不但可以抓取网页的内容,还可以实时更新读取的进度。读者尝试读取一个较大的网页,看看百分比的更新情况。
PageTask task = new PageTask();
task.execute(url.getText().toString());
* Params 启动任务执行的输入参数,比如HTTP请求的URL。
* Progress 后台任务执行的百分比。
* Result 后台执行任务最终返回的结果,比如String。
AsyncTask 的执行分为四个步骤,与前面定义的TaskListener类似。每一步都对应一个回调方法,需要注意的是这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法。在任务的执行过程中,这些方法被自动调用。
* onPreExecute() 当任务执行之前开始调用此方法,可以在这里显示进度对话框。
* doInBackground(Params...) 此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间。在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。
* onProgressUpdate(Progress...) 此方法在主线程执行,用于显示任务执行的进度。
* onPostExecute(Result) 此方法在主线程执行,任务执行的结果作为此方法的参数返回。
PageTask扩展了AsyncTask,在 doInBackground()方法中读取网页内容。PageTask的源代码如下所示:
// 设置三种类型参数分别为String,Integer,String
class PageTask extends AsyncTask<String, Integer, String> {
// 可变长的输入参数,与AsyncTask.exucute()对应
@Override
protected String doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
// params[0] 代表连接的url
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
String s = null;
if (is != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[128];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
baos.write(buf, 0, ch);
count += ch;
if (length > 0) {
// 如果知道响应的长度,调用publishProgress()更新进度
publishProgress((int) ((count / (float) length) * 100));
}
// 为了在模拟器中清楚地看到进度,让线程休眠100ms
Thread.sleep(100);
}
s = new String(baos.toByteArray()); }
// 返回结果
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
// 返回HTML页面的内容
message.setText(result);
}
@Override
protected void onPreExecute() {
// 任务启动,可以在这里显示一个对话框,这里简单处理
message.setText(R.string.task_started);
}
@Override
protected void onProgressUpdate(Integer... values) {
// 更新进度
message.setText(values[0]);
}
}
复制代码
执行PageTask非常简单,只需要调用如下代码。重新运行NetworkActivity,不但可以抓取网页的内容,还可以实时更新读取的进度。读者尝试读取一个较大的网页,看看百分比的更新情况。
PageTask task = new PageTask();
task.execute(url.getText().toString());
发表评论
-
IMF简介-2
2013-07-09 17:56 845InputManager 由UI控件(View,TextVie ... -
IMF简介-1
2013-07-09 17:51 771Input Method Framework 目录 ... -
输入法 总结-3
2013-07-08 15:02 814public class MainActivity exten ... -
输入法 总结-4
2013-07-04 17:29 896public class InputActivity exte ... -
输入法 总结-3
2013-07-04 17:28 825public class InputApplication e ... -
输入法 总结-2
2013-07-04 17:23 732openwnn_pref_ja.xml <?xml v ... -
输入法 总结-2
2013-07-04 17:21 13openwnn_pref_ja.xml <?xml v ... -
输入法 总结-1
2013-07-04 17:19 762<application android ... -
Android快捷键
2013-03-15 14:39 762Home键(小房子键) 在 ... -
如何通过wifi调试android程序
2013-02-27 15:48 706android手机居然可以通过wifi进行程序的调试,太好了, ... -
Please ensure that adb is correctly located at 'D:\android-sdk-windows\platform-
2013-02-27 14:27 1598adt 出现ADB server didn't ACK, fa ... -
Android ViewPager 左右滑动2-1
2013-01-29 10:12 1398public class PagerActivity exte ... -
Android ViewPager 左右滑动-3
2013-01-29 10:10 1055<?xml version="1.0" ... -
Android ViewPager 左右滑动-2
2013-01-29 10:10 1394/*** * 选中效果 */ pu ... -
Android ViewPager 左右滑动-1
2013-01-29 10:09 1328Store extends ActivityGroup imp ... -
Android 下载
2013-01-27 00:59 863下载apk程序代码 protected File downLo ... -
Android SDCard操作-3
2013-01-26 18:31 840public static Intent getVid ... -
Android SDCard操作-2
2013-01-26 18:30 1024/** * 判断该应用在手机中的安装情况 ... -
Android SDCard操作-1
2013-01-26 18:27 987private static int INSTALLED = ... -
Android GridView显示图片
2013-01-05 22:52 6053package com.example.dynamicimag ...
相关推荐
Android AsyncTask的简单Demo,对应Blog文章:http://blog.csdn.net/dolacmeng/article/details/50215519
Android AsyncTask用法和异步加载图片通过AsyncTask 从网络上异步加载一张图片
Android AsyncTask Demo 自己参考网络写的学习 demo
Android AsyncTask异步处理下载网页
代码为博客的示例代码,有问题请博客中留言:http://blog.csdn.net/lmj623565791/article/details/38614699
AsyncTask 断点下载
Android AsyncTask 文章集合
android AsyncTask中更新progressBar 。可用于http请求、下载文件等
Android Asynctask 先小试牛刀,了解一下各个方法执行过程,关注博客http://himici.com/
android AsyncTask的简单使用方法
android asyncTask的使用,通过简单的实例来说明Task异步类的使用
Android AsyncTask 同步下载图片,先是显示默认的图标,下载完成了立刻显示下载的图标,可以左右切换
android asynctask的fragment更新UI(附线程管理) 博客地址:http://blog.csdn.net/qq_16064871
Android AsyncTask Demo
简单的android AsyncTask 例子
android AsyncTask介绍,多线程。
主要为大家详细解析了Android AsyncTask使用以及源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
自己写了一个基于httpurlconnection和Asynctask的例子
里面是Android AsyncTask 的两种使用方法,一种是加载网络图片,一种是模拟耗时加载进度条
如果用现在做秒表,建议用asyncTask...