How to Make an HTTP Connection via Code Part 2

Page content

This article follows what we started in “How to Make a BlackBerry HTTP Connection via Code: Part 1”.

Let’s talk about the two other ways we left without explanation in Part 1.

BlackBerry Internet Service:

This type of connection is available only to BlackBerry Alliance Program members. Any user is allowed to have their BIS transport activated but only third party developers who are members of the BlackBerry Alliance Program will be provided with the option of developing an application with HTTP requests which use the BIS channel. Once you have checked you are part of this alliance you have to ask for your application to be allowed to use this transport type.

After that your application could make HTTP connections via code through the BIS service by adding the following suffix at the end of the connection string.

(HttpConnection)“; deviceside=false;ConnectionType=mds-public”);

Wireless Service Provider WAP 2.0 gateway:

To make an HTTP connection using the WAP 2.0 gateway, first we have to locate its service record on the BlackBerry service book and using its ConnectionUID value build the string.

We first get all the service records and then filter them by their connection id (cid). Wi-Fi and MMS records also have a WPTCP cid so we also have to filter them.

Once the proper UID is found, we just have to add it at the end of the connection string as follows:

ServiceBook sb = ServiceBook.getSB(); ServiceRecord[] records = sb.findRecordsByCid(“WPTCP”); String uid = null; for(int i=0; i < records.length; i++) { //Search through all service records to find the //WAP 2.0 Gateway Service Record. if (records[i].isValid() && !records[i].isDisabled()) { if (records[i].getUid() != null && records[i].getUid().length() != 0) { if ((records[i].getUid().toLowerCase().indexOf(“wifi”) == -1) && (records[i].getUid().toLowerCase().indexOf(“mms”) == -1)) { uid = records[i].getUid(); break; } } } } if (uid != null) { //open a WAP 2 connection (HttpConnection)“;ConnectionUID=" + uid); } else { //Do another thing. }

Tips and recommendations

It’s strongly recommended to be very careful when dealing with connections to control the status of the connection so as to preserve the mobile battery status and avoid weird behaviours on your applications. It’s also recommended to check the available services to the service book before trying to make the connection and you can also check for coverage for any transport type you are going to use. This will avoid unsuccessful attempts to connect and will optimize the network usage.

Also take care of the cost that an HTTP connection may incur. Some transport will be included in a data plan but some won’t. A loop checking the available services before any connection could be the best solution, and also remember to use threads different from the main thread in any connection you try so as to not block it for future operations.