Mike D
Mike D

Reputation: 4946

Blackberry HttpConnection Timing Out

I am trying to grab an XML file for parsing with the following:

private void getAndParseXML( String _xmlurl ) {
        HttpConnection xmlcon = null;
        InputStream input     = null;
        SAXParserFactory spf  = null;

        try {
            xmlcon = (HttpConnection)Connector.open( _xmlurl, Connector.READ ); // open connection to XML source
            spf = SAXParserFactory.newInstance();                               // set up xml parsers
            input = xmlcon.openInputStream();                                   // set up input stream

            SAXParser saxparser = spf.newSAXParser();   // create a new parser object
            saxparser.parse( input, this );             // parse operations start here
        }
        catch( IOException ex ) {
            System.out.println( "IOException Caught:\t" + ex.getMessage() ); // set a default item if any exception occurs with retreiving or parsing XML file
        }
        catch (SAXException ex) {
            System.out.println( "SAXException Caught:\t" + ex.getMessage() );
            ex.printStackTrace();
        }
        catch ( IllegalArgumentException ex ) {
            System.out.println( "IllegalArgumentException Caught:\t" + ex.getMessage() );
            ex.printStackTrace();
        }
        catch (ParserConfigurationException ex) {
            System.out.println( "ParserConfigurationException Caught:\t" + ex.getMessage() );
            ex.printStackTrace();
        }
        finally {

            if ( input != null) {
                try {
                    input.close();  // attempt to close all connections

                }
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if ( xmlcon != null ) {
                try {
                    xmlcon.close();
                }
                catch ( IOException ex ) {
                    ex.printStackTrace();
                }
            }
        }
    } // END ----------------------------------------------------------------------------

But the I get an exception thrown saying the connection timed out after 12 seconds. This after the line input = xmlcon.openInputStream(); is executed.

If this is relevant, it is the IOException that gets caught, and determining if there is an active network connection is done before this method is called. Did I miss something?

EDIT: Just for clarification, this would be the first instance of a network connection in the application. Before this block of code, a simple test:

private boolean isConnectedToNetwork() {

        boolean isConnected = false;

        if ( (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_CELLULAR)) || (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI)) )
            if ( (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_CELLULAR)) || (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI)) )
                isConnected = true;

        return isConnected;
    }

to make sure a connection would be possible, before attempting to retrieve an XML file.

Upvotes: 1

Views: 766

Answers (2)

Mike D
Mike D

Reputation: 4946

Found the issue. The url, in this case _xmlurl, needed to be appended with ";deviceside=true" to ensure a direct TCP/IP connection was established. This makes sure an HttpConnection is made through the cellular network. In other words, to make sure the connection was not made through the Blackberry MDS.

Also, a check was needed:

if ( (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI)) && (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI)) )

the wi-fi antenna was on. If the above ervaluated to true, the url (again _xmlurl) needed to be further appended with ";interface=wifi" to avoid the cellular network , but still open a direct TCP/IP connection.

Upvotes: 1

Vit Khudenko
Vit Khudenko

Reputation: 28418

Mike, everything looks OK.

However here are some ideas to think of:

  1. Can you open the URL from your browser?
  2. What BB Transport do you use to open the connection (for instance, maybe it fails on BES, but will work Ok on Direct TCP or Wi-Fi)?
  3. By the moment of this call, have the code that "determins if there is an active network connection" closed all connections it might have opened (if any) during detection?

Upvotes: 1

Related Questions