《Android开发之异步猎取并下载网络资源_.docx》由会员分享,可在线阅读,更多相关《Android开发之异步猎取并下载网络资源_.docx(9页珍藏版)》请在三一文库上搜索。
1、Android开发之异步猎取并下载网络资源_ 1)从指定的URL猎取对应的流 既然要猎取网络资源,那么首先得有个URL,那么这里我首先封装一个打开URL连接猎取到的InputStream 流,这样一来无论是图片资源还是文本文件资源都可以用法该接口方法来猎取流。 该功能主要应用URLConnection和HttpURLConnection来实现,具体实现方案如下: 代码如下: private InputStream openHttpConnection(String urlString) throws IOException InputStream in = null; int response
2、 = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if(!(conn instanceof HttpURLConnection) throw new IOException(It is not an HTTP connection); try HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowR
3、edirects(true); httpConn.setRequestMethod(GET); httpConn.connect(); response = httpConn.getResponseCode(); if (response = HttpURLConnection.HTTP_OK) in = httpConn.getInputStream(); catch (Exception ex) Log.v(Networking,ex.getLocalizedMessage(); throw new IOException(Error connecting); return in; 代码如
4、下: (2)封装了上面的猎取流方法接口后,我们就可以利用上面封装的方法来猎取并下载相应图片和文本文件内容了 猎取并下载图片资源方法: 代码如下: private Bitmap downloadImage(String url) Bitmap bitmap = null; InputStream in = null; try in = openHttpConnection(url); bitmap = BitmapFactory.decodeStream(in); in.close(); catch (IOException e) / TODO Auto-generated catch bloc
5、k Log.v(NetworkingActivity, e.getLocalizedMessage(); return bitmap; 代码如下: 猎取并下载文本内容方法: 代码如下: private String downloadText(String url) int BUFFER_SIZE = 2021; InputStream is = null; try is = openHttpConnection(url); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); return ;
6、InputStreamReader isr = new InputStreamReader(is); int charRead; String str=; char inputBuffer = new charBUFFER_SIZE; try while(charRead=isr.read(inputBuffer)0) String readString = String.copyValueOf(inputBuffer, 0, charRead); str += readString; inputBuffer = new charBUFFER_SIZE; is.close(); catch (
7、IOException e) / TODO Auto-generated catch block e.printStackTrace(); return ; return str; 代码如下: (3)在猎取下载图片资源和文本内容资源方法都完成后,现在就可以开头下载任务了,为了防止等待效应,一般采纳异步任务来下载网络资源。 对对应的下载资源任务封装各自的异步下载任务即可。下面就是实现异步下载任务的方案: 异步下载图片任务: 代码如下: private class DownloadImageTask extends AsyncTaskString, Bitmap, Long long images
8、Count = 0; ProgressBar progressBar; public DownloadImageTask(ProgressBar pBar) this.progressBar = pBar; Override protected Long doInBackground(String. urls) / TODO Auto-generated method stub for(int i = 0; i urls.length;i+) Bitmap imageDownloaded = downloadImage(urlsi); if(imageDownloaded!=null) ima
9、gesCount +; publishProgress(imageDownloaded); try Thread.sleep(300); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); return imagesCount; /display the image downloaded Override protected void onProgressUpdate(Bitmap. bitmaps) / TODO Auto-generated method stub ivI
10、mg.setImageBitmap(bitmaps0); progressBar.setProgress(int) imagesCount*10); /when all the images have been downloaded Override protected void onPostExecute(Long imageDownloaded) / TODO Auto-generated method stub String str = 下载完成!一共下载了+imagesCount +张图片; Toast.makeText(getBaseContext(), str, Toast.LEN
11、GTH_SHORT).show(); 代码如下: 异步下载文本文件内容任务: 代码如下: private class DownloadTextTask extends AsyncTaskString, Void, String Override protected void onPostExecute(String result) / TODO Auto-generated method stub Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show(); Override protected String doInBackground(String. urls) / TODO Auto-generated method stub return downloadText(urls0); 代码如下: 这样一来,异步下载网络资源就完成了。 下面为了读者便利测试,下面供应本文实例代码中的相关网络资源URL,以便利大家自己测试用法。其余非核心代码就不在贴出来,望读者见谅。 代码如下: /图片下载URLs private String mUrl = ; /异步下载文本文件内容任务 new DownloadTextTask().execute(strUrl); 更多信息请查看IT技术专栏 .