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: 16
Registered: ‎06-27-2012
My Device: Bold 9900
My Carrier: Bell

Download XML file from webserver.

I know this has probably been done to death, but I'm having a hard time grabbing an XML file from a webserver within my app. The app creates the file, but it is 0kb, and I later get a RuntimeException from the catch block (weird, I know). Here is my source, it is the same method as was discussed in this thread:

 

 public void downloadFile(String remoteFile, String localfile)
        {
        	int startByte = 0;
        	int bunchSize = 100000;
        	try
        	{
        		FileConnection dest = (FileConnection)Connector.open(localfile,Connector.READ_WRITE);
        		if (!dest.exists()) {
        			dest.create();
        		}
        		OutputStream outStream = dest.openOutputStream();
        		
        		Dialog.inform("File Created, starting download");
        		
        		while(true)
        		{

        			HttpConnection connection = null;
        			InputStream inputStream = null;

        			connection = (HttpConnection) Connector.open(remoteFile, Connector.READ_WRITE, true);

        			connection.setRequestProperty("Range", "bytes=" + startByte + "-" + ((startByte += bunchSize) -1));
        			inputStream = connection.openInputStream();

        			//byte[] data = IOUtilities.streamToBytes(inputStream);
        			//Dialog.alert(String.valueOf(data.length));

        			int responseCode = connection.getResponseCode();
        			if (responseCode != 200 && responseCode != 206)
        			{
        				break;
        			}

        			byte[] Buffer = new byte[1024];
        			int length = -1;
        			while ((length = inputStream.read(Buffer)) > 0) {
        				outStream.write(Buffer, 0 , length);
        			}

        			connection.close();
        		} // end while
        		
        		outStream.close();
        	}
        	catch ( Exception e )
        	{
        		Dialog.alert(e.toString());
        	}
        } 

Could anyone give me a push in the right direction? The file does exist on the server.

Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: Bold 9900
My Carrier: Bell

Re: Download XML file from webserver.

Seems the problem lies in this line:

inputStream = connection.openInputStream();

Maybe it's throwing an IOException because my app doesn't have the right permission to be writing to a file? Which is weird cause I certainly have the right permissions to create a file...

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Download XML file from webserver.

This sort of network procesing must not be done on the Event Thread.  But if you have moved it off the Event Thread, then issuing a Dialog.alert will cuase an IllegalStateException, in your catch.  I suggest you use soms other form of logging in here. 

 

I would just step through this code debugging it, I am sure you can figure out the problem that causes the Exeption in the first place. 

Contributor
Posts: 16
Registered: ‎06-27-2012
My Device: Bold 9900
My Carrier: Bell

Re: Download XML file from webserver.

[ Edited ]

yeah sorry, I fixed that problem literally the second you commented here.

I've narrowed it down to the connection timing out. The error I'm getting is "local connection timed out after ~120000"

 

Edit: Simon Hain has already solved my problem Smiley Happy

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Download XML file from webserver.

Have you read further on that Thread to see that the solution is for the SImulator and may not work on device?