Android 实现联网——在线程中联网

来源:互联网 发布:google 图片压缩算法 编辑:程序博客网 时间:2024/05/09 04:04

在Android 实现联网的时候,必须考虑到数据堵塞问题。比如:从网络上下载一张图片: public Bitmap returnBitmap(String url) { URL myFileUrl = null; Bitmap bitmap = null; try{ myFileUrl = new URL(url); }catch(MalformedURLException e){ e.printStackTrace(); return null; }; try{ HttpURLConnection conn = (HttpURLConnection)myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactroy.decodeStream(is); is.close(); }catch(IOException e){ e.printStackTrace(); } return bitmap; } 由于网络连接需要很长的时间,需要3-5秒,甚至更长的时间才能返回页面的内容。如果此连接动作直接在主线程,也就是UI线程中处理,会发生什么情况呢?整个程序处于等待状态,界面似乎是“死”掉了。为了解决这个问题,必须把这个任务放置到单独线程中运行,避免阻塞UI线程,这样就不会对主线程有任何影响。举个例子如下: private void connect(String strURL){ new Thread() { public void run() { 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; } s = new String(baos.toByteArray()); Log.V(“moandroid sample”,s); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } 使用Handler更新界面如何将下载的信息显示在界面上了,比如说下载的进度。Android SDK平台只允许在主线程中调用相关View的方法来更新界面。如果返回结果在新线程中获得,那么必须借助Handler来更新界面。为此,在界面Activity中创建一个Handler对象,并在handleMessage()中更新UI。 //Task在另外的线程执行,不能直接在Task中更新UI,因此创建了Handler private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { String m = (String) msg.obj; message.setText(m); } }; 只需要将上面的 Log.V(“moandroid sample”,s); 替换为: s = new String(baos.toByteArray()); Message mg = Message.obtain(); mg.obj = s; handler.sendMessage(mg); AsyncTask 看上去修改后的connect()方法已经可用了,但是这种匿名程的方式是存在缺陷的: 线程的开销较大,如果每个任务都要创建一个线程,那么应用程 序的效率要低很多; 线程无法管理,匿名线程创建并启动后就不受程序的控制了,如果有很多个请求发送,那么就会启动非常多的线程,系统将不堪重负。 另外,前面已经看到,在新线程中更新UI还必须要引入handler,这让代码看上去非常臃肿。 为了解决这一问题,Android在1.5版本引入了AsyncTask。AsyncTask的特点是任务在主线程之外运行,而回调方法是在主线程中执行,这就有效地避免了使用Handler带来的麻烦。阅读AsyncTask的源码可知,AsyncTask是使用java.util.concurrent 框架来管理线程以及任务的执行的,concurrent框架是一个非常成熟,高效的框架,经过了严格的测试。这说明AsyncTask的设计很好的解决了匿名线程存在的问题。 AsyncTask是抽象类 定义了三种泛型类型 Params,Progress和Result。 Params 启动任务执行的输入参数,比如HTTP请求的URL。 Progress 后台任务执行的百分比。 Result 后台执行任务最终返回的结果,比如String。 子类必须实现抽象方法doInBackground(Params… p) ,在此方法中实现任务的执行工作,比如连接网络获取数据等。通常还应该实现onPostExecute(Result r)方法,因为应用程序关心的结果在此方法中返回。需要注意的是AsyncTask一定要在主线程中创建实例。 AsyncTask的执行分为四个步骤,每一步都对应一个回调方法,需要注意的是这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法。在任务的执行过程中,这些方法被自动调用,运行过程,如下图所示: onPreExecute() 当任务执行之前开始调用此方法,可以在这里显示进度对话框。 doInBackground(Params…) 此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间。在执行过程中可以调用publicProgress(Progress…)来更新任务的进度。 onProgressUpdate(Progress…) 此方法在主线程执行,用于显示任务执行的进度。 onPostExecute(Result) 此方法在主线程执行,任务执行的结果作为此方法的参数返回。 举个简单的例子如下: // 设置三种类型参数分别为String,Integer,String class PageTask extends AsyncTask { // 可变长的输入参数,与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非常简单,只需要调用如下代码。 PageTask task = new PageTask(); task.execute(url.getText().toString()); 总结说明 Handler在前面我们已经学习过,今天突然看到AsyncTask,以及学习其他人的博客基础上,做出了上面的总结,感觉自己收获很多,在这里与大家分享。

转载:http://blog.csdn.net/mingxunzh/archive/2010/03/08/5357942.aspx

原创粉丝点击