Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 23
Registered: ‎08-09-2010
My Device: 9800
My Carrier: Airtel

How to download images asynchronously from web server

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.

 

    list.png

I have tried few approaches so far,

Approach 1:

- 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.

Approach 2:

-  While searching for the above issue, I came across WebBitmapField in blackberry from coderholic.com.
-  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.

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

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.

Can anyone please help me to understand and implement the thread pooling for blackberry for my current problem?

Also, I appreciate anyone giving me any other best approach which will fit for my requirement.

Thanks in advance.

Developer
Posts: 17,017
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to download images asynchronously from web server

i would suggest to use a listfield. you can update the underlying data structure and invalidate the row or the field to show the changes.

using so many threads to download does not really help as the underlying framework does not utilize them efficiently, i would limit it to 3 maybe.
we use a runnable-queue for downloads and other processing off the event thread.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 23
Registered: ‎08-09-2010
My Device: 9800
My Carrier: Airtel

Re: How to download images asynchronously from web server

[ Edited ]

As I am a kind of newbie can you please explain how i can do it with list view?

 

Can call downloadImage() method from drawRow() method before drawing of each image? or how I should do this ?

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: How to download images asynchronously from web server

 

Hi @nilanchalapanigrahy

 

The preferred approach is #2.

 

You would have to implement your own manager which would maintain a list of active threads and start new ones only when available (#activeThreads < MAX_NUM_OF_THREADS).

 

If you would search the forum, you will find few threads regarding this issue.

 

If you need assistant implementing the manager, post again with the code which you already wrote for it.

 

E.

Contributor
Posts: 23
Registered: ‎08-09-2010
My Device: 9800
My Carrier: Airtel

Re: How to download images asynchronously from web server

Hello @maadani, I truely appriciate for your quick reply. I am using the below link which explains creating of own manager. Can you please help me to understand how could I change it to fix for my requirement. I mean how could I create pooling mechanism ?

 

http://www.coderholic.com/blackberry-webbitmapfield/

 

 

Developer
Posts: 17,017
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to download images asynchronously from web server

you cannot call the download from drawRow as the download is always done asynchronically. You trigger the download of the picture, if the download is finished you put it into the data structure for the listfield and refresh the listfield.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: How to download images asynchronously from web server

[ Edited ]

Here is a good example to to write a ListField, as mentioned by Simon:

http://developerlife.com/tutorials/?p=898

 

Now, the link that you gave is only for one field and you would need a manager that would maintain a list (or a runnable queue).

 

Here is a link for a thread pool:

IMPORTANT NOTE: not specifically for BB but it will get you started...

http://www.nloko.ca/?p=300

 

Do note that you would need to pass a callback object to your manager that would be called once the download is done.

 

E.