如何从Web服务器下载图像异步 [英] How to download images asynchronously from web server

查看:127
本文介绍了如何从Web服务器下载图像异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序界面类似于附有影像。我有多个行,每一行都有一个位图图像,标题和描述字段。所有信息的越来越从XML的形式支持我们的web服务器牵强。现在,我已经使用观察者设计模式,这对于连接到我的远程服务器通过HTTP下载创建一个单独的线程并解析XML。该XML包括图像,标题和描述每行的网址。

My application screen looks similar to the image attached. I have multiple rows and each row has a Bitmap image, title and description field. All the information's are getting fetched from our supporting web-server in the form of XML. Now, I have used observer design pattern, which creates a separate thread for connecting to my remote server over HTTP, downloads and parse the XML. The XML includes the URL for image, title and description for each row.

我曾尝试几种方法,到目前为止,

I have tried few approaches so far,

方法1:


  • 创建一个单独的方法(drawRow()),这需要通过指定的布局将内容一起照顾。

  • 而且,然后使用方法downloadImage(),我试图从drawRow()方法下载远程URL。 但它吮吸,因为它使用下载同一个线程和UI都会被阻止。

  • Created a separate method (drawRow()), which takes care of putting the contents together by specifying the layout.
  • And, then using the method downloadImage(), I am trying to download the remote URL from the drawRow() method. But it sucks, as it downloads using the same thread and UI gets blocked.

方法二:

在寻找针对上述问题,我碰到 WebBitmapField 黑莓来自 coderholic.com

While searching for the above issue, I came across WebBitmapField in blackberry from coderholic.com.

和,然后我使用下面的code从我drawRow()方法。据我了解的WebBitmapField,这里采用观察者设计模式,图像被下载了超过UI线程其他线程。它正常工作时,我有限的行数像5或10。但是,当我有更多的行数要绘制它抛出 TooManyThreads 例外,因为它创造了一个新的线程对于每一行

And, then I am using the below code from my drawRow() method. As I understand the WebBitmapField, here is using observer design pattern and the image is downloading over thread other than UI thread. It works fine when I have limited number of rows like 5 or 10. But when I have more number of rows to be drawn it throws TooManyThreads exception, as it creates a new thread for each row.

我有这个链接 taskworker线程黑莓,但不与如何实现我的要求太多清晰。

I have got this link taskworker-thread-blackberry, but not much clear with how to achieve my requirement.

据我了解,在黑莓应用程序可以创建最多16个线程数。所以,现在我相信了。我可能需要创建一个线程池指定最大尺寸为10。

As I understand, in blackberry an application can create maximum of 16 number of threads. So, now I believe. I may need to create a thread pool specifying max size to 10.

任何人都可以请帮助我理解和执行线程池黑莓对我目前的问题?

另外,我AP preciate任何人给我的,这将适合我的要求的其他最好的办法。

先谢谢了。

推荐答案

您有你需要的一切。所以:

You have everything what you need. So:


  1. 为应用程序创建有一个 TaskWorker (使用singelton)

  2. 实施工作从类 TaskWorker - DownloadImageTask (简单地把一切从 Runnable.run() Task.doTask()法)

  3. 而不是新的线程创建Util.getWebData()通话 TaskWorker.addTask()

  1. Create one TaskWorker for your application (use singelton)
  2. Implement Task class from TaskWorker - DownloadImageTask (simply put everything from Runnable.run() to Task.doTask() method)
  3. Instead of new thread creation in Util.getWebData() call TaskWorker.addTask()

有可能更微小的细节,但你可以弄清楚如何完成它。

There are probably more minor details but you could figure out how to finish it.

和我认为这是最好有两个方法回调 - 成功(字节[]数据)错误(错误的Throwable) - 确定最终的结果和逃生图像转换为字符串和背部

And I think it's better to have two method in Callback - success(byte[] data) and error(Throwable error) - to determine end result and escape converting images to String and back.

这篇关于如何从Web服务器下载图像异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆