Http Connection Problem

Everything else that does not fit into the forums below.

Http Connection Problem

Postby arunk on Mon Aug 25, 2008 2:21 pm

Hi ,

I am developing a blackberry application which reads an xml file from a website.

In blackberry there are different ways by which we can connect to internet..

So I want some help in doing that..

I want to know which type of internet connection is available and according to that connect to the internet ..


Arun
User avatar
arunk
 
Posts: 4
Joined: Fri Aug 22, 2008 10:46 pm

Re: Http Connection Problem

Postby k on Mon Aug 25, 2008 6:32 pm

arunk wrote:Hi ,

I am developing a blackberry application which reads an xml file from a website.

In blackberry there are different ways by which we can connect to internet..

So I want some help in doing that..

I want to know which type of internet connection is available and according to that connect to the internet ..


Arun


Hi Aarun,

welcome to our forum. I think you are accessing the XML file from a web server. Therefor, an HTTPConnection would make sense for you to grab the contents of the file. I am a bit lazy so I copied just the original example from RIM (or is it from SUN?):

Code: Select all
     void getViaHttpConnection(String url) throws IOException {
         HttpConnection c = null;
         InputStream is = null;
         int rc;

         try {
             c = (HttpConnection)Connector.open(url);

             // Getting the response code will open the connection,
             // send the request, and read the HTTP response headers.
             // The headers are stored until requested.
             rc = c.getResponseCode();
             if (rc != HttpConnection.HTTP_OK) {
                 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();
             if (len > 0) {
                 int actual = 0;
                 int bytesread = 0 ;
                 byte[] data = new byte[len];
                 while ((bytesread != len) && (actual != -1)) {
                    actual = is.read(data, bytesread, len - bytesread);
                    bytesread += actual;
                 }
             } else {
                 int ch;
                 while ((ch = is.read()) != -1) {
                     ...
                 }
             }
         } catch (ClassCastException e) {
             throw new IllegalArgumentException("Not an HTTP URL");
         } finally {
             if (is != null)
                 is.close();
             if (c != null)
                 c.close();
         }
     }


So it is pretty much straight forward for you:
  1. Open Connection to your XML file.
  2. Open InputStream.
  3. Read from InputStream.
  4. Parse your XML.
  5. Close InputStream.
  6. Close Connection.

I guess that's it 8-)

Check out the HTTPConnection doc: http://www.blackberry.com/developers/docs/4.3.0api/javax/microedition/io/HttpConnection.html
BlackBerryInsight - Lead Editor

Blog English: www.blackberryinsight.com | Chinese: cn.blackberryinsight.com | German: de.blackberryinsight.com | French: fr.blackberryinsight.com
Forum: forums.blackberryinsight.com
User avatar
k
Site Admin
 
Posts: 150
Joined: Wed Jul 30, 2008 1:30 pm

Re: Http Connection Problem

Postby arunk on Mon Aug 25, 2008 8:07 pm

I did the same and its working in the Simulator using MDS support.

But its not working in the device...

I am using a gprs connection from Airtel...

When I appended the WAPgatewayAPN and wapgatewayIP to the url I was able to connect to the internet..

For that I had a service book entry named Wap Transport in my device.

When I used the same application with different service providers like, T mobile or vodafone its not working

Exception happens inthe following code

c = (HttpConnection)Connector.open(url);
User avatar
arunk
 
Posts: 4
Joined: Fri Aug 22, 2008 10:46 pm

Re: Http Connection Problem

Postby k on Mon Aug 25, 2008 11:04 pm

arunk wrote:I did the same and its working in the Simulator using MDS support.

But its not working in the device...

I am using a gprs connection from Airtel...

When I appended the WAPgatewayAPN and wapgatewayIP to the url I was able to connect to the internet..

For that I had a service book entry named Wap Transport in my device.

When I used the same application with different service providers like, T mobile or vodafone its not working

Exception happens inthe following code

c = (HttpConnection)Connector.open(url);


which exception?
BlackBerryInsight - Lead Editor

Blog English: www.blackberryinsight.com | Chinese: cn.blackberryinsight.com | German: de.blackberryinsight.com | French: fr.blackberryinsight.com
Forum: forums.blackberryinsight.com
User avatar
k
Site Admin
 
Posts: 150
Joined: Wed Jul 30, 2008 1:30 pm

Re: Http Connection Problem

Postby arunk on Tue Aug 26, 2008 1:40 pm

IOException: Invalid tunnel name
User avatar
arunk
 
Posts: 4
Joined: Fri Aug 22, 2008 10:46 pm

Re: Http Connection Problem

Postby k on Tue Aug 26, 2008 2:28 pm

arunk wrote:IOException: Invalid tunnel name


So there are two possible reasons for this:
  1. You tunnel name is correct (e.g. APN setup is wrong)
  2. Your SD-Card is not set up for data connections
BlackBerryInsight - Lead Editor

Blog English: www.blackberryinsight.com | Chinese: cn.blackberryinsight.com | German: de.blackberryinsight.com | French: fr.blackberryinsight.com
Forum: forums.blackberryinsight.com
User avatar
k
Site Admin
 
Posts: 150
Joined: Wed Jul 30, 2008 1:30 pm

Re: Http Connection Problem

Postby arunk on Tue Aug 26, 2008 3:41 pm

I know that its the poroblem with the Apn settings..

Can you please tell me how to get the Apn settings programatically...

(Apn and IP address) for a valid connection
User avatar
arunk
 
Posts: 4
Joined: Fri Aug 22, 2008 10:46 pm

Re: Http Connection Problem

Postby k on Tue Aug 26, 2008 5:00 pm

arunk wrote:I know that its the poroblem with the Apn settings..

Can you please tell me how to get the Apn settings programatically...

(Apn and IP address) for a valid connection


AFAIK this is not possible.

In case you figure out how that works I would be happy if you drop me a note.
BlackBerryInsight - Lead Editor

Blog English: www.blackberryinsight.com | Chinese: cn.blackberryinsight.com | German: de.blackberryinsight.com | French: fr.blackberryinsight.com
Forum: forums.blackberryinsight.com
User avatar
k
Site Admin
 
Posts: 150
Joined: Wed Jul 30, 2008 1:30 pm

Re: Http Connection Problem

Postby Hendrik on Thu Aug 28, 2008 12:23 pm

A apn settings list for data connections is available at pinstack
http://www.pinstack.com/carrier_setting ... teway.html

But use this list with caution, as any carrier can change its settings any time.
Also this list is by no means complete, for example the apn for CSL Hong Kong prepaid SIM card is not listed here. it's CSLP1 for CSL prepaid.

What You could do is parse this document and create some kind of table for lookup. Once the phone knows it's carrier it can look up the correct APN setting. Seems to be a lot of work however.
Unluckily it's not possible to grab the APN settings directly as k already mentioned. If You do have a Blackberry plan APN should be pushed on Your Blackberry in the TCP settings. So no need for an APN in case You do a TCP connection.
User avatar
Hendrik
Moderator
 
Posts: 21
Joined: Mon Aug 04, 2008 11:51 am


Return to BlackBerry Everything

Who is online

Users browsing this forum: No registered users and 0 guests