android asynctack 异步任务
这里写一个小实例,来学习巩固android asynctack 异步任务的知识,以便在项目中使用。
介绍一下如何使用
1, 继承asynctask
public class mytask extends asynctask<params, progrss, result>
我们来说一下这三个泛型的作用:
params: 调用异步任务时传入的类型 ;
progress : 字面意思上说是进度条, 实际上就是动态的由子线程向主线程publish数据的类型
result : 返回结果的类型
2, 重写这个类的抽象方法doinbackground, 当然它也有几个方法需要重写, 我们一一看来
doinbackground(抽象方法, 必须实现)
/* 唯一执行在子线程中的方法
* 所以不可以进行ui的更新
* @param params
* @return
*/
@override//返回值: result 参数: param
protected string doinbackground(textview... params) {
text = params[0];
random random = new random();
for (int i = 0; i < 50; i++) {
//要进行进度的更新
publishprogress(i);
//不能直接调用onprogressupdate方法,
//这样会使得onprogressupdate在子线程中运行
try {
thread.sleep(random.nextint(10) * 10);
} catch (interruptedexception e) {
e.printstacktrace();
}
}
return "已完成";
}
下面三个方法根据具体情况选择使用
//执行doinbackground之前调用
@override
protected void onpreexecute() {
super.onpreexecute();
}
@override//与publishprogress(i)对应
protected void onprogressupdate(integer... values) {
super.onprogressupdate(values);
text.settext(string.valueof(values[0]));
}
//在doinbackground之后执行
@override // 参数s为 result
protected void onpostexecute(string s) {
super.onpostexecute(s);
text.settext(s);
}
3, 执行异步任务
有两种方式, 我已经把区别写在了注释中 /* 直接execute异步任务, 都是同一线程去执行 */ text = (textview) findviewbyid(r.id.main_text1); new mytask().execute(text); text = (textview) findviewbyid(r.id.main_text2); new mytask().execute(text); text = (textview) findviewbyid(r.id.main_text3); new mytask().execute(text); text = (textview) findviewbyid(r.id.main_text4); new mytask().execute(text);
/* 启动多条线程来执行异步任务 api11以上可以使用 */ scheduledthreadpoolexecutor executor = new scheduledthreadpoolexecutor(4); text = (textview) findviewbyid(r.id.main_text1); new mytask().executeonexecutor(executor, text); text = (textview) findviewbyid(r.id.main_text2); new mytask().executeonexecutor(executor, text); text = (textview) findviewbyid(r.id.main_text3); new mytask().executeonexecutor(executor, text); text = (textview) findviewbyid(r.id.main_text4); new mytask().executeonexecutor(executor, text);
注意: 如果我们直接去execute我们的任务, 它(任务) 只会在同一个子线程中运行, 所以上述第一种方式启动时, 四个任务顺次执行(就是一个任务执行完了再执行另一个); 而第二种方式, 给它创建了线程池, 这样会自动给它创建新的子线程, 所有的任务不是顺序执行, 而是几个线程”同时执行”
获取网络数据呈现在webview和下载图片和其共存的案例
1, 首先我们要来一个布局, 具体需求是这样的, 在webview之上有个imageview, 并且, imageview可以随webview滚动, 所以这个时候我们想到了用scrollview, 但是大家一定不要忘记, scrollview只能包含一个控件, 所以我们可以用linearlayout包裹一下即可
<scrollview
android:layout_width="match_parent"
android:layout_height="match_parent">
<linearlayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<imageview
android:id="@+id/main2_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<webview
android:id="@+id/main2_web"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</linearlayout>
</scrollview>
2, 接下来我们要有一个实体类, 用来存放从网页上下载的内容(这里加注解原因在于我们要使用gson解析来自网页的内容)
public class entry {
@serializedname("title")
private string title;
@serializedname("message")
private string message;
@serializedname("img")
private string image;
public string gettitle() {
return title;
}
...//省略其余getter和setter方法
public void setimage(string image) {
this.image = image;
}
}
3, 那我们接下解决的问题就是 如何下载图片? 如何下载web内容? , 那我们写两个通用的工具类
下载工具类(通用型)
/**
* created by lulu on 2016/8/31.
* <p/>
* 通用下载工具类
*/
public class networktask<t> extends asynctask<networktask.callback<t>, void, object> {
private networktask.callback<t> callback;
private class<t> t;
private string url;
public networktask(string url, class<t> t) {
this.url = url;
this.t = t;
}
@override
protected object doinbackground(callback<t>... params) {
callback = params[0];
try {
httpurlconnection connection = (httpurlconnection) new url(url).openconnection();
connection.setrequestmethod("get");
connection.setdoinput(true);
int code = connection.getresponsecode();
if (code == 200) {
inputstream is = connection.getinputstream();
bytearrayoutputstream bos = new bytearrayoutputstream();
byte[] buffer = new byte[102400];
int length;
while ((length = is.read(buffer)) != -1) {
bos.write(buffer, 0, length);
}
return bos.tostring("utf-8");
} else {
return new runtimeexception("服务器异常");
}
} catch (exception e) {
e.printstacktrace();
return e;
}
}
@override
protected void onpostexecute(object o) {
super.onpostexecute(o);
if(o instanceof string) {
string str = (string) o;
gson gson = new gson();
t t = gson.fromjson(str, this.t);
callback.onsuccess(t);
}
if( o instanceof exception) {
exception e = (exception) o;
callback.onfailed(e);
}
}
public interface callback<s> {
void onsuccess(s t);
void onfailed(exception e);
}
}
图片加载器(通用型)
/**
* created by lulu on 2016/8/31.
* 图片网络加载器
* 下载成功返回bitmap
* 否则返回null
*/
public class imageloader extends asynctask<string, void, bitmap>{
private imageview image;
public imageloader(imageview image) {
this.image = image;
image.setimageresource(r.mipmap.ic_launcher);
}
@override
protected void onpreexecute() {
super.onpreexecute();
}
@override
protected bitmap doinbackground(string... params) {
string url = params[0];
try {
httpurlconnection connection = (httpurlconnection) new url(url).openconnection();
connection.setrequestmethod("get");
connection.setdoinput(true);
int code = connection.getresponsecode();
if (code == 200) {
inputstream is = connection.getinputstream();
return bitmapfactory.decodestream(is);
}
} catch (ioexception e) {
e.printstacktrace();
}
return null;
}
@override
protected void onpostexecute(bitmap bitmap) {
super.onpostexecute(bitmap);
if (bitmap != null) {
image.setimagebitmap(bitmap);
} else {
image.setimageresource(r.mipmap.failed);
}
}
}
4, 测试activity
注意: 看如何解决大图在webview中不左右滑动的问题!
public class main2activity extends appcompatactivity implements networktask.callback<entry>{
private webview web;
private imageview image;
//解决大图在webview中不左右滑动的问题
private static final string css = "<style>img{max-width:100%} </style>";
private string title;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main2);
web = (webview) findviewbyid(r.id.main2_web);
image = (imageview) findviewbyid(r.id.main2_image);
new networktask<>("http://www.tngou.net/api/top/show?id=13122", entry.class).execute(this);
}
@override
public void onsuccess(entry t) {
web.loaddatawithbaseurl("", t.getmessage(), "text/html; charset=utf-8", "utf-8", null);
new imageloader(image).execute("http://img.blog.csdn.net/20160829134937003");
}
@override
public void onfailed(exception e) {
web.loaddatawithbaseurl("", "加载失败", "text/html; charset=utf-8", "utf-8", null);
}
}
5.效果图:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!