如何从Web服务器下载图像异步 [英] How to download images asynchronously from web server
问题描述
我的应用程序界面类似于附有影像。我有多个行,每一行都有一个位图图像,标题和描述字段。所有信息的越来越从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:
- 为应用程序创建有一个
TaskWorker
(使用singelton) - 实施
工作
从类TaskWorker
-DownloadImageTask
(简单地把一切从Runnable.run()
到Task.doTask()
法) - 而不是新的线程创建Util.getWebData()
在
通话 TaskWorker.addTask()
- Create one
TaskWorker
for your application (use singelton) - Implement
Task
class fromTaskWorker
-DownloadImageTask
(simply put everything fromRunnable.run()
toTask.doTask()
method) - Instead of new thread creation in
Util.getWebData()
callTaskWorker.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屋!