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
New Developer
Posts: 134
Registered: ‎01-11-2011
My Device: curve

How to download in a loop

Hi all,

 

In my app i need to download some images from server.I am doing this  like:-

 

HttpConnection connection = null;
			InputStream inputStream = null;
			int length = list.size();
			try {
				
				for (int i = 0; i < length; i++) {
					String URL = "My URL";
					connection = (HttpConnection) Connector.open(URL
							Connector.READ, true);
					inputStream = connection.openInputStream();

					byte[] responseData = IOUtilities
							.streamToBytes(inputStream);

					EncodedImage image = EncodedImage.createEncodedImage(
							responseData, 0, responseData.length);
					
					vector.add(image);
					inputStream.close();
					connection.close();
				}
				
				
				
			} catch (Exception e) {
				Logger.log("exception e:" + e.getMessage());
			}

 

is i am doing in the correct way or is there any other better way to do this.

 

Developer
Posts: 134
Registered: ‎08-03-2011
My Device: torch, Z10
My Carrier: Vodafone, Airtel

Re: How to download in a loop

[ Edited ]

i think it is correct. you can use like this also.

 

 

 

public final class MyScreen extends MainScreen{

	String url = "http://i49.tinypic.com/5oftpv.jpg";
	public MyScreen() {
		
		ButtonField btn =new ButtonField("Download Bitmap");
		btn.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				for (int i = 0; i < 5; i++) {
					BitmapField bmp = new BitmapField(connectServerForImage(url));
					add(bmp);
				}
			}
		});
		add(btn);
	}

	public static Bitmap connectServerForImage(String url) {

		HttpConnection httpConnection = null;
		DataOutputStream httpDataOutput = null;
		InputStream httpInput = null;
		int rc;

		Bitmap bitmp = null;
		try {
			httpConnection = (HttpConnection) Connector.open(url);
			rc = httpConnection.getResponseCode();
			if (rc != HttpConnection.HTTP_OK) {
				throw new IOException("HTTP response code: " + rc);
			}
			httpInput = httpConnection.openInputStream();
			InputStream inp = httpInput;
			byte[] b = IOUtilities.streamToBytes(inp);
			EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
			return hai.getBitmap();

		} catch (Exception ex) {
			System.out.println("URL Bitmap Error........" + ex.getMessage());
		} finally {
			try {
				if (httpInput != null)
					httpInput.close();
				if (httpDataOutput != null)
					httpDataOutput.close();
				if (httpConnection != null)
					httpConnection.close();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return bitmp;
	}
}

 

-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to download in a loop

Brajeshs code accesses the network on the event thread, you should not do that.
see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

@OP: The code looks fine in theory, but you can get into issues with the underlying framework, i think you cannot re-use the same connection object quickly as the framework cleans it up first. (not sure about that, just experience)
Try to use a local connection variable and null it after closing.
----------------------------------------------------------
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
Highlighted
New Developer
Posts: 134
Registered: ‎01-11-2011
My Device: curve

Re: How to download in a loop

Hi simon_hain

 

my code is working fine.No problems at all.

Only a message is getting print on console that a thread  is already in process.

that why i am asking you.

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

Re: How to download in a loop

this message usually means that your previous application instance was not properly shut down, for example due to listeners you did not unregister on exit.
----------------------------------------------------------
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