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
HanneSThEGreaT
Posts: 34
Registered: ‎07-30-2013
My Device: Curve 9300

Downloading a file

hello guys.

 

I am trying to implement this into my code :

 

http://supportforums.blackberry.com/t5/Java-Development/Download-large-files-using-the-BlackBerry-Mo...

 

But once run, the program doesn't do anything. :smileysad:

 

It is not a large file that has to be downloaded ( 984 KB )

 

I just want a way to download the file once a user clicks a button, without any further needed input.

 

Can anyone help please?

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

Re: Downloading a file

please post your relevant code. do you get anything in the logs? any exceptions?
----------------------------------------------------------
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
HanneSThEGreaT
Posts: 34
Registered: ‎07-30-2013
My Device: Curve 9300

Re: Downloading a file

The code in my first post did absolutely nothing :smileysad: No errors etc., it just sat there.

I am now instead trying to implement this code :

 

CLASS :

 

package com.ver.ncc; 
import javax.microedition.io.HttpConnection;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

public class ProspectusDownloader {
ProspectusDownloader() {
}
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();
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();
}
outStream.close();
}
catch ( Exception e ) {
}
}
}

 ffound here : http://supportforums.blackberry.com/t5/Java-Development/Download-Files/td-p/533139/highlight/true/pa...

 

My call to this class :

 

		 ProspectusDownloader pd = null; 		 		 pd.DownloadFile("http://www.ncc-cla.com/downloads/NCCVer-Prospectus.pdf", "NCC_Prospectus"); 

 Now it tells me error 104 null reference.

 

How can I implement the above code in my app?

 

I just want to download a file

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

Re: Downloading a file

first, make sure that your code runs in a separate thread.
see
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

i don't see where you exit your while(true) loop
----------------------------------------------------------
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
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Downloading a file

How large is the file you are trying to download?  If you are downloading a file that is less than 64K, then you can ignore 'range'.  If you are downloading a file larger than 64 KB, you might, depending on the transport, get it in one hit.  Alternatively, you might want to use range, but that assumes an Http 1.1 Server - which you are likely to have but it is something to check. 

 

When you say you get a 104, is that your responseCode, or does your application just die with this error code.  I suspect the later, in which case run it in debug mode, and find out which line has the null reference. 

 

You connection code looks a little out of order and a little odd.  I think it would be good for you to have a little more understanding of Blackberry networking.  So I recommend you review this page:

 

http://developer.blackberry.com/bbos/java/documentation/intro_networking_1984362_11.html

 

I generally develop for OS 5.0 and above, and so I recommend you use ConnectionFactory API, which is documented here:

http://developer.blackberry.com/bbos/java/documentation/network_api_1984363_11.html

Developer
HanneSThEGreaT
Posts: 34
Registered: ‎07-30-2013
My Device: Curve 9300

Re: Downloading a file

The size is about 800 KB.

I get the 104 on this line :

pd.DownloadFile("http://www.ncc-cla.com/downloads/NCCVer-Prospectus.pdf", "NCC_Prospectus");

I will have a look at the connectionfactory as well
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Downloading a file

In most cases you will probably get away with a single download, but I would use a range.

 

But initially, while you are developnig your code, you can use a simple single get, becuase the SImualtor will not be restricted. 

 

If you are getting a NPE on that line, then pd is null.  Have you done a

pd = new ....