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
Developer
walkline
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900
Accepted Solution

Get image via httpConnection very slowly

I have been wroted a function to get a internet image, the image size always in 50x50px, very small, but I have to waste  a long time to reveiving data, below is my code:

 

package code;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.io.IOCancelledException;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.ui.UiApplication;


public class HttpUtility
{
	private static byte[] _imageData;
	private static String _url;
	
	public static byte[] getInternetImage(String url, boolean waiting)
	{
		_url=url;
		_imageData=null;

		Runnable run=new Runnable()
		{
			public void run()
			{
				byte[] data=null;
				
				try {
					HttpConnection httpConnection=(HttpConnection) Connector.open(_url+Function.updateConnectionSuffix());
					InputStream inputStream=httpConnection.openInputStream();
					
					if(inputStream.available()>0)
					{
						data=IOUtilities.streamToBytes(inputStream);
						inputStream.read(data);
						
						_imageData=data;
					}
				} catch(IOCancelledException e2) {Function.errorDialog(e2.toString());}
				  catch (IOException e) {Function.errorDialog(e.toString());}
			}
		};

		if(waiting)
		{
			UiApplication.getUiApplication().invokeAndWait(run);	
		} else {
			UiApplication.getUiApplication().invokeLater(run);
		}
		
		return _imageData;
	}
}

 The updateConnectionSuffix() used to determine the network environment and choose a best way for httpConnection.

 

Is there any problem in my code? I have no idear

Developer
simon_hain
Posts: 16,364
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Get image via httpConnection very slowly

invokelater runs the code on the event thread, not on a separate thread.
currently you are blocking your UI until the data is available.

see
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

take a look at the httpdemo to see how you could do it better.
----------------------------------------------------------
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
walkline
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900

Re: Get image via httpConnection very slowly

Addtional question is, I must use invokeAndWait() method to wait data receiving, if I'm using invokeLater(), I will never got the correct data, is there any idears for this issue?

Developer
walkline
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900

Re: Get image via httpConnection very slowly

thanks for the GFW in my conutry, I can not load the video in my browser......

 

simon, there is a question with new thread, whether the thread invoke and wait? I means that I have to wait the data to fill my bitmapField in next segment, if the new thread not invoke and wait, I'm not sure what kind of the data I have been received.

Developer
simon_hain
Posts: 16,364
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Get image via httpConnection very slowly

usually you utilize a callback to get notified by the thread that the data is available.
you do not wait in your UI, but react on the data availability.
----------------------------------------------------------
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
walkline
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900

Re: Get image via httpConnection very slowly

thanks for your advise, simon, I will try it

 

Developer
walkline
Posts: 84
Registered: ‎01-10-2013
My Device: Bold 9900

Re: Get image via httpConnection very slowly

simon, I did it, thanks very much!!!~