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: 10
Registered: ‎01-16-2013
My Device: BB7 and BB10
My Carrier: Airtel

To make an HTTP call in BB 7.1

Hello Friends,

 

I am developing an app for blackberry 7.1  using java eclipse plugin.

 

I need to hit an URL having query string and parse the xml response which I get back from there.

 

How do i acieve it.

 

I am unable to figure out a working code.

 

*** dummy url****

http://198.162.1.183/webpay/neft?login=160&pass=Test@123&ttype=CCFundTransfer

 

for this url I am supposed to get one xml response.

 

Firstly I need to hit this and get the response and then parse this XML.

 

 

Can anybody please help me out with this.

 

 String URL = "http://198.162.1.183/webpay/neft?login=160&pass=Test@123&ttype=CCFundTransfer ";
       
        HttpConnection c = null;
   InputStream is = null;
   int rc;
   OutputStream os = null;

   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.
       c.setRequestMethod(HttpConnection.POST);
       c.setRequestProperty("If-Modified-Since",
           "29 Oct 1999 19:43:31 GMT");
       c.setRequestProperty("User-Agent",
           "Profile/MIDP-2.0 Configuration/CLDC-1.0");
       c.setRequestProperty("Content-Language", "en-US");

       // Getting the output stream may flush the headers
       os = c.openOutputStream();
       os.write("LIST games\n".getBytes());
       os.flush();           // Optional, getResponseCode will flush

       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();
      // processType(type);
       // 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();
   }
  

 

 

I know I am missing out something here or the approach is wrong.

 

Please help.

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

Re: To make an HTTP call in BB 7.1

Welcome to the forums.

 

Not looked at your code, just replying quickly.

 

For the URL query, have you looked at htmlDemo sample.  It is a good place to start.  It also does it processing off the Event Thread.  Processing network communication on the Event Thread is a common mistake for new developers. 

 

For background information, especially regarding the 'connection string', you should also review the following:

http://supportforums.blackberry.com/t5/Java-Development/Networking-Transports-II/ta-p/446742

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

and then review ConnectionFactory that you will find documented in the API. 

And if you are planning on supporting OS 6.0 and above only, you might like to review Destination/DestinationFactory. 

 

For the XML parsing, this should help:

http://supportforums.blackberry.com/t5/Java-Development/Use-the-XML-Parser/ta-p/445210

 

As you might expect, similar questions have been asked before, I recommend that you use the Search facility and try to find previous questions and their responses before posting your own.  The Search box is near the top right hand side. 

 

Good luck. 

 

Contributor
Posts: 10
Registered: ‎01-16-2013
My Device: BB7 and BB10
My Carrier: Airtel

Re: To make an HTTP call in BB 7.1

Thanks for the reply.

 

I went through those links, however my program is getting struck in the the last line (mentioned in bold):

 

c.setRequestMethod(HttpConnection.POST);

c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded";charset=UTF-8");

rc = c.getResponseCode();

 

Not sure if  this problem is coming because of content type.

 

My server is suppose to return me an XML file if I submit a query URL.

 

Thanks for reading,

Sunaina

 

 

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

Re: To make an HTTP call in BB 7.1

The problem here is almost certainly because you are running this on a Simulator.  On a Simulator, unless you specify a connection string, it will try to use the MDS SImulator which you probably have not started. 

 

To get round this, on the Simulator, I always code the connectionString ";deviceside=true'.  I suspect if you add this to your URL, your program will start working.  As it is, I suspect if you left it for 2 minutes, it would eventually time out and throw an exception. You should have a try catch around the code you have shown, put a breakpoint in the catch and you will see this.  Note that you can detect you are running on the Simulator using

DeviceInfo.isSimulator();

 

The following KB article that I pointed you at previously explains what ";deviceside=true" means. 

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

 

Add the ";deviceside=true" connection string to get it going on the Simulator for now.  Longer term you will need to code connection strings varying depending on the connection method you want to use.  The options are explained in the video I pointed you at, how to set the option you want to use is in the KB article referenced above.  But I recommend that you use ConnectionFactory rather than trying to code your own connection string that will need to vary depending on the connection options available for the device.