BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 03-05-2010, 01:39 PM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2009
Model: 8300
PIN: N/A
Carrier: Rogers
Posts: 43
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Proper error checking for HttpConnection. Help! :)

Please Login to Remove!

Hey Guru's,

I am somewhat new to bb development although I'v been coding in other languages for a long time. I have a blackberry app that I made the fetches from the web some data (to display to the user).

The problem is sometimes the request to the web for information doesn't work. This could be anything from them being out of the service area, packet loss, I dunno. But sometimes it just doesn't return any data. I close the app and retry and then it magically works.

So, I am wondering what is wrong with my code below. I found it on the web and it appeared to check for issues. However, I have never seen any error messages pop up when data wasn't returned.

I did notice the connection timeout was set to 120000 (2 minutes I believe)... seems a bit high.

I appreciate any advice you can give.

Code:
try {
                HttpConnection c = null;
                InputStream is = null;
                int rc;

                try {
                
                    c = (HttpConnection)Connector.open("http://myurl/getData.asp?id=123;ConnectionTimeout=120000;deviceside=false");

                    rc = c.getResponseCode();
                    if (rc != HttpConnection.HTTP_OK) {
                        Dialog.alert("There was a problem downloading the ticket info.");
                        throw new IOException("HTTP response code: " + rc);
                    }
        
                    is = c.openInputStream();
        
                    // Get the ContentType
                    String type = c.getType();

                    // Get the length and process the data
                    int len = (int)c.getLength();

                    int ch;
                    StringBuffer sb = new StringBuffer();
                    while ((ch = is.read()) != -1) {
                        sb.append((char)ch);
                    }

                } catch (ClassCastException e) {
                    Dialog.alert("There was a problem downloading the ticket info.");
                    throw new IllegalArgumentException("Not an HTTP URL");
                } catch (InterruptedIOException e) {
                    Dialog.alert("There was a problem downloading the ticket info!");
                    throw new IllegalArgumentException("Connection timed out!");
                } finally {
                    if (is != null)
                        is.close();
                    if (c != null)
                        c.close();
                }
            } catch (IOException e) {
            } catch (Exception e)
            {
                e.printStackTrace();
            }
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

1PC used CEL-30AP 220V 2.6KW server #2
$270.78
1PC used CEL-30AP 220V 2.6KW server   #2 pictureHP Agilent JetDirect J4100A 400N J4100-60002 Print Server Tested
$49.99
HP Agilent JetDirect J4100A 400N J4100-60002 Print Server Tested pictureServer 3 Pocket Waitress Waist Apron Tiny Mod Flowers Name FREE Lady Pizazz
$12.0
Server  3 Pocket Waitress Waist Apron Tiny Mod Flowers  Name  FREE Lady Pizazz pictureIntel sr2500 2RU 19inch rack mounted server + rocketport card. COA etc
$120.56
Intel sr2500 2RU 19inch rack mounted server + rocketport card. COA etc pictureEVS XT / XT2 4/6 channel LSM slo mo server with open config and more.
$6697.63
EVS XT / XT2   4/6 channel LSM slo mo server with open config and more. picture






Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.