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
justindutoit
Posts: 396
Registered: ‎05-31-2009
My Device: Not Specified
Accepted Solution

Try to connect to internet for X seconds, then give up in code

Hey. If for any reason at all an internet request cannot succeed after a certain number of seconds, I need my app to give up and return an error code. I have code as below. The only thing I could think of was to set up a ten second countdown, then close the connection, but that seems bodgy.

 

What is the best way to do this? Tks

 

Justin.

 

    private static byte[] GetByteArrayFromURL(String url) throws IOException {
        
         HttpConnection c = null;
         
         InputStream is = null;
         int rc;
         byte[] returnVal = null;
         String suffix = ";deviceside=false";
          
        try {
             c = (HttpConnection)Connector.open(url + suffix);   

             // Getting the response code will open the connection,
             // send the request, and read the HTTP response headers.
             // The headers are stored until requested.
             
             String logMessage = "About to getResponseCode...";
             EventLogger.logEvent(0x4c9d3452d87922f8L, logMessage.getBytes(), EventLogger.ALWAYS_LOG);
             
             rc = c.getResponseCode();    // << Here the attempt begins
             
             if (rc != HttpConnection.HTTP_OK) {
                 logMessage = "Error in request..." + rc;

 

 

 

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Try to connect to internet for X seconds, then give up in code

There's an (undocumented, as far as I can tell) parameter ConnectionTimeout=n that you can add to the connection suffix, where n is milliseconds. You also need to use the three-argument form of Connector.open, with the third parameter set to true, to receive timeout exceptions in your code.

 

If you're using JDE 5, you can use the ConnectionFactory class to do this in a much more straightforward way. Just call setConnectionTimeout(long) before getting the connection.

 

By-the-by, ten seconds is a pretty short time-out. This is a mobile device, after all.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
justindutoit
Posts: 396
Registered: ‎05-31-2009
My Device: Not Specified

Re: Try to connect to internet for X seconds, then give up in code

Thanks :smileyhappy: What would you think for a timeout to download 40K? I guess it depends on the country and many things... BTW I used 0 as the access mode.

 

Justin

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Try to connect to internet for X seconds, then give up in code

Connection time-outs aren't related to download size, at least not directly. A timeout occurs when there is not a timely response from an upstream server. (E.g., the device sends out a packet, expecting a response, and nothing comes back.)

 

I'm guessing that using 0 for the access mode is treated as Connection.READ_WRITE (since 0 is not a defined access mode and READ_WRITE is the default). I'd recommend explicitly using READ_WRITE for the access mode. I vaguely recall reading something about potential problems if you don't, and I don't think there are any down sides if you do.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.