【IT168 技术文档】连接互联网的能力大大提升了移动电话的业务范围,中国移动很多出色的业务,像移动随身听,飞信等都是基于网络连接的应用程序,这也说明未来的移动互联网领域大有可为。因此深入掌握的联网应用程序的开发和设计至关重要。本文主要介绍在开发联网应用过程中如何管理线程,如何从主线程之外更新界面。(作者:中国移动通信研究院 詹建飞)
为什么需要线程
假设需要开发一个联网应用程序,需要从一个网址抓取网页内容,这里读取的网页地址是笔者在本地机器上自己建立的服务器地址。当然在读取网页内容的时候,可以使用HttpClient提供的API,但是这并不是本文的介绍重点。缺乏联网程序开发经验的程序员可能写出下面的代码。
package com.ophone.network;
//这里为了节省篇幅,忽略了import项
public class NetworkActivity extends Activity {
// 显示任务的执行状态和返回结果
private TextView message;
private Button open;
private EditText url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
message = (TextView) findViewById(R.id.message);
url = (EditText) findViewById(R.id.url);
open = (Button) findViewById(R.id.open);
open.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
connect();
}
});
}
private String connect() {
try {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url.getText().toString());
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;
// 为了在模拟器中清楚地看到进度,让线程休眠1000ms
Thread.sleep(50000);
}
s = new String(baos.toByteArray());
}
// 返回结果
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
//这里为了节省篇幅,忽略了import项
public class NetworkActivity extends Activity {
// 显示任务的执行状态和返回结果
private TextView message;
private Button open;
private EditText url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
message = (TextView) findViewById(R.id.message);
url = (EditText) findViewById(R.id.url);
open = (Button) findViewById(R.id.open);
open.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
connect();
}
});
}
private String connect() {
try {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url.getText().toString());
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;
// 为了在模拟器中清楚地看到进度,让线程休眠1000ms
Thread.sleep(50000);
}
s = new String(baos.toByteArray());
}
// 返回结果
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
网络连接通常是比较耗时的,尤其是在当前的GPRS这种低速率的网络情况下,这样connect()方法可能需要3-5秒,甚至更长的时间才能返回页面的内容。如果此连接动作直接在主线程,也就是UI线程中处理,会发生什么情况呢?为了在模拟器中更好的模拟网络读取速度慢的情况,笔者在读取过程中让线程休眠了50秒,运行NetworkActivity,点击“连接”按钮。意外发生了,按钮长时间没有反应,整个界面似乎是“死”掉了。系统随后显示出了 ANR(应用程序无响应)错误提示,如图1所示:
