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: 41
Registered: ‎11-08-2011
My Device: Blackberry Torch
My Carrier: ATT

ConnectionFactory.getConnection() Returns Null (Sometimes)

I have an application that issues HTTP requests.  After a little while of doing this, it eventually has an issue.  ConnectionFactory.getConnection() returns Null.  I wish it threw an exception so it would be clearer why its doing this after working fine over and over.  

 

I should note that this issue only seems to happen on a real phone.  Any ideas?

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Hi @andemartini

 

 

The ConnectionFactory can return null if no transport is available or no Transport options are set.

 

Try to see which of the transport causes you the error and see and the full URL contains the relevant parameters.

 

Also, please note that there is a known issue when the ConnectionFactory tries to use the BIS transport without using the BISOptions class (the getConnection returns null).

 

E.

 

 

Contributor
Posts: 41
Registered: ‎11-08-2011
My Device: Blackberry Torch
My Carrier: ATT

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Thank you for your response.  I'm always calling getConnection with the same URL but it doesn't always work.  After a while it starts to return null.  This happens regardless of whether I include "deviceside=true;interface=wifi" in the URL.  I should note that I'm connecting to a server over WIFI.  Are there other options that I should be setting?

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Use this method and bellow class...

 

 String connectionString = connection.getConnectionString();
                     if(connectionString == null)
                        {
                         System.out.println("No connection string found.");
                           
                        }
                     String targetUrl = YOUR URL + connectionString;
                
                    
                   HttpConnection httpConn = (HttpConnection)Connector.open(targetUrl);
                   

 

public class connection {

     private static final boolean USE_MDS_IN_SIMULATOR = false;

    public static String getConnectionString()
    {
       
        String connectionString = null;                
    
        // Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
        if(DeviceInfo.isSimulator())
        {
                if(Popiconnection.USE_MDS_IN_SIMULATOR)
                {
                       System.out.println("Device is a simulator and USE_MDS_IN_SIMULATOR is true");
                        connectionString = ";deviceside=false";
                }
                else
                {
                        System.out.println("Device is a simulator and USE_MDS_IN_SIMULATOR is false");
                        connectionString = ";deviceside=true";
                }
        }                                        
    
        // Wifi is the preferred transmission method
        else if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
        {
            System.out.println("Device is connected via Wifi.");
            connectionString = ";interface=wifi";
        }
    
        // Is the carrier network the only way to connect?
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
        {
            System.out.println("Carrier coverage.");
    
            String carrierUid = getCarrierBIBSUid();
            if(carrierUid == null)
            {
                // Has carrier coverage, but not BIBS.  So use the carrier's TCP network
                System.out.println("No Uid");
                connectionString = ";deviceside=true";
            }
            else
            {
                // otherwise, use the Uid to construct a valid carrier BIBS request
                System.out.println("uid is: " + carrierUid);
                connectionString = ";deviceside=false;connectionUID="+carrierUid + ";
            }
        }                
    
        // Check for an MDS connection instead (BlackBerry Enterprise Server)
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
        {
           System.out.println("MDS coverage found");
            connectionString = ";deviceside=false";
        }
    
        // If there is no connection available abort to avoid bugging the user unnecssarily.
        else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
        {
            System.out.println("There is no available connection.");
        }
    
        // In theory, all bases are covered so this shouldn't be reachable.
        else
        {
           System.out.println("no other options found, assuming device.");
            connectionString = ";deviceside=true";
        }        
    
        return connectionString;
    }
    
    /**
     * Looks through the phone's service book for a carrier provided BIBS network
     * @return The uid used to connect to that network.
     */
    private static String getCarrierBIBSUid()
    {
        ServiceRecord[] records = ServiceBook.getSB().getRecords();
        int currentRecord;
    
        for(currentRecord = 0; currentRecord < records.length; currentRecord++)        
        {             if(records[currentRecord].getCid().toLowerCase().equals("ippp"))            
        {                 if(records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0)
                {
                    return records[currentRecord].getUid();
                }
            }
        }
    
        return null;
    }

    
}

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

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

If you use ConnectionFactory, then you should not be coding the connection string extensions to your URL - the ConnectionFactory adds these for you.

 

The code supplied by VimalSandy is interesting and might be useful if you decide not to use ConnectionFactory.  Personally I would stick with ConnectionFactory.  If however you decide to use the code, then please move the test for MDS before anything else (including WiFi), otherwise I suspect you will have problems on corporate phones. 

Developer
Posts: 135
Registered: ‎11-14-2010
My Device: BlackBerry Z10 (White)
My Carrier: Vodafone

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Did you ever resolve your issue andemartini? I am having exactly the same problem. ConnectionFactory.getConnection() returns a ConnectionDescriptor object most of the time but after probably 20 successful connections it returns null. It seems pretty random as to when it returns null.

Contributor
Posts: 41
Registered: ‎11-08-2011
My Device: Blackberry Torch
My Carrier: ATT

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Hi donturner,

 

Make sure you close all connections (both the sockets and the Connection object) or else eventually ConnectionFactory will give you null.  This was my problem.  Hope this helps.

 

Anthony

Developer
Posts: 135
Registered: ‎11-14-2010
My Device: BlackBerry Z10 (White)
My Carrier: Vodafone

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Many thanks for the quick response. I've spent days trying to stop ConnectionFactory.getConnection() from returning null and despite closing all connections it still does. In the end I've reverted to generating the connection strings manually and that has solved all the issues. 

 

I can only assume that ConnectionFactory does some connection caching and does not close those connections in a timely manner. Kind of frustrating that ConnectionFactory is unreliable at creating connections! 

Contributor
Posts: 31
Registered: ‎11-14-2012
My Device: Blackberry 9780
My Carrier: Digicel

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Hey donturner, 

 could you post some code? I'm trying to use the connection strings now... I'm trying to force TCP because that was fastest when using the connection descriptor but now I'm basically getting nothing. My connections just time out. 

Highlighted
Contributor
Posts: 31
Registered: ‎11-14-2012
My Device: Blackberry 9780
My Carrier: Digicel

Re: ConnectionFactory.getConnection() Returns Null (Sometimes)

Here's some snippets of code...

if(useConnectionFactory){
httpConnection = (HttpConnection) connectionDescriptor
.getConnection();
} else {
String connectionString = getConnectionString();
String targetUrl = url + connectionString;
httpConnection = (HttpConnection) Connector.open(targetUrl);
}

// then later...

int status = httpConnection.getResponseCode();

 

Here's my getConnectionString method. I basically force it so that it returns "deviceside=true;" to force it to use TCP. Is this correct? WAP was extremely slow. Is there a way I could get the connection string from the factory to see what they were doing differently?

 

public static String getConnectionString() {

String connectionString = null;

// Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR
// variable.
if (DeviceInfo.isSimulator()) {

// Don't use MDS
connectionString = ";deviceside=true";
}

// Wifi is the preferred transmission method
else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
System.out.println("Device is connected via Wifi.");
connectionString = ";interface=wifi";
}

// Is the carrier network the only way to connect?
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
System.out.println("Carrier coverage.");

String carrierUid = null; //getCarrierBIBSUid(); // <<<<============== FORCE TCP
if (carrierUid == null) {
// Has carrier coverage, but not BIBS. So use the carrier's TCP
// network
System.out.println("No Uid");
connectionString = ";deviceside=true;";
} else {
// otherwise, use the Uid to construct a valid carrier BIBS
// request
System.out.println("uid is: " + carrierUid);
connectionString = ";deviceside=false;connectionUID="
+ carrierUid + ";";
}
}

// Check for an MDS connection instead (BlackBerry Enterprise Server)
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
System.out.println("MDS coverage found");
connectionString = ";deviceside=false";
}

// If there is no connection available abort to avoid bugging the user
// unnecssarily.
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
System.out.println("There is no available connection.");
}

// In theory, all bases are covered so this shouldn't be reachable.
else {
System.out.println("no other options found, assuming device.");
connectionString = ";deviceside=true";
}

return connectionString;
}