BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-25-2008, 06:07 AM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Model: 8110
PIN: N/A
Carrier: Airtel
Posts: 1
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default What is the best way to programmatically get APN/Gateway IP and make a HTTP Connectio

Please Login to Remove!

Hi All,

I have written a small BB application to access a HTTP URL. I am using the regular HTTPConnection class for this (Code Attached). This is the behavior observed.

1. My application works fine with a simulator using MDS.

2. My Provider does not provide access to a BES server. However, my phone had a valid APN and Gateway IP in the WAPTransport[WAP]. Hence I modified my application to pick up this information and establish a connection over GPRS. In this case the connection establishment did work.

My question is as follows:

I also tried to run my application on a BB in another network. On this phone there is no entry with the name WAPTransport[WAP] in the Service Book. In this case my application fails (gives an exception on the line marked with RED). But other applications such as the browser continue to work. Where do these applications get the APN/Gateway IP from?

Appreciate any help in this regard,

Thanks in Advance,
Prasad


Code:
public class ReadHttpFile extends Thread {
         HttpConnection httpConnection = null;
         InputStream inputStream = null;
         int responseCode;
         public boolean isInvalidFile=false;
         String url;
         StringBuffer URLString;

        ReadHttpFile(String URL){
            url = URL;
            URLString = new StringBuffer();
            URLString.append(url);
            try{

                ServiceBook serviceBook = ServiceBook.getSB();
                ServiceRecord[] records =
serviceBook.findRecordsByCid("WAP");
                for(int i = 0; i < records.length; i++){
                    if(records[i].getName().equalsIgnoreCase("WAP
Transport")){
                        URLString.append(";WAPGatewayIP=");
                        String networkAddress =
records[i].getNetworkAddress();
                        URLString.append(networkAddress.substring(0,networkA
ddress.indexOf(':')));
                        URLString.append(";WAPGatewayAPN=");
                        URLString.append(records[i].getAPN());
                        url = URLString.toString();
                    }
                }


            }catch(Exception e){
                System.out.println("Exception : "+e.toString());
            }

        }


        public void run() {
            try{
                ReadData(url);
            }catch(IOException ie){
                System.out.println("IOException : "+ie);
            }catch(Exception e){
                System.out.println("Exception : "+e);
            }

        }

            public void ReadData(String url) throws IOException{
                try {

                        httpConnection =
(HttpConnection)Connector.open(url,Connector.READ);

                        if(httpConnection != null){
                            httpConnection.setRequestMethod(HttpConnection.P
OST);
                            httpConnection.setRequestProperty("Content-Langu
age", "en-US");
                            httpConnection.setRequestProperty("Content-Type"
, "text/xml; charset=utf-8");
                            responseCode = httpConnection.getResponseCode();
                            if (responseCode != HttpConnection.HTTP_OK) {
                                System.out.println("Http connection :
Failed");
                            }else{
                                System.out.println ("Http connection : OK
");
                                inputStream =
httpConnection.openInputStream();
                                StringBuffer sb = new StringBuffer();
                                int ch;
                                while ((ch = inputStream.read()) != -1) {
                                    sb.append((char) ch);
                                }
                                System.out.println("XML "+ sb .toString());
                           }
                        }else{
                            System.out.println("Connection object : Null ");
                        }

                    }catch(IOException ioe){
                        System.out.println("Exception : "+ ioe.toString());
                    }catch(Exception e){
                        System.out.println("Exception : "+ e.toString());
                    } finally {
                        if (inputStream != null)
                            inputStream.close();
                        if (httpConnection != null)
                            httpConnection.close();
                    }

        }

}
Offline  
Old 08-25-2008, 09:21 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi!

You can find a listing of APNs and appropriate carrier-specific information at a number of places on the Internet. Use Google, and do a search on BlackBerry APN settings. Pinstack and BlackBerryFAQ have good lists.

As for how the browsers know what to use without having it specified in the TCP settings, my guess is as follows: the carrier-specific browser (e.g., Media.Net for AT&T/Cingular) already knows what the proper APN settings must be (doh!). So I'd guess that they just hard-code it.

The BlackBerry browser is a different story. It could know the carrier-specific APN settings (you can find out who the carrier is via the Vendor API) for each carrier - there aren't that many of them, and RIM would know what their settings are. Another possibility is that the BlackBerry browser avoids them altogether and uses its own hard-coded APN information, through a server that they maintain.

Cheers,

karl
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Old 08-25-2008, 02:23 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Dec 2005
Model: 8310
Carrier: Rogers
Posts: 138
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's good to know about the various connection modes available to you before you whip up connection code.

They are:
WIFI - You can make a wifi connection using the service book record (there are other ways but you have to be an alliance member to have access to that information)
BES - deviceside=false
BIS - You need to have your application approved by RIM alliance to use this or they will block your traffic to and from whatever server you're running.
WAP - You can make a wap connection from the associated service book record (you can use the connectionUID=<wap record uid> parameter. RIM made this information public in a tutorial video so it is no longer covered by their NDA)
Direct TCP - You need the apn information. The string is deviceside=true;apn=<apn>

Also some more detail about browsers:

There are 3 (main) types of browsers: BES browser, BIS browser and WAP browser. Generally speaking, most corporate users will be using their BES browser, which tunnels all traffic through a BES. Consumer users will likely be using a BIS browser, and that goes through the carrier's BIS, which RIM moderates. WAP browsers use the wap service record stored on the phone to make the connection. These service records contain references to native routines and the OS code knows how to connect to the various wap gateways. You can also manually specify wap gateway ip's and ports yourself.

You can manually load the browser with a service record and have it use the associated connection type.

Last edited by CELITE : 08-25-2008 at 02:25 PM.
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





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