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
New Contributor
dorelberar
Posts: 3
Registered: ‎02-26-2009
My Device: Not Specified

Blackberry OS 7 - Multiple HTTP Connections Failure

Hi all,

 

I'm porting an application which runs OK on Blackberry OS 6 (and earlier) to Blackberry OS 7.

 

The application opens multiple HTTP connections (at the same time) to a server that hangs the response until the data is available (uses continuation).

 

I've studied lots of HTTP Implementations on this forum and other sites, and tried to adapt the code but with no luck so far.

 

I stumbled upon various exceptions (Tested using 9810 and 9930 Simulators)

 

*  When Using Wi-Fi (Simulator & Device)

The exception below happens after Several Prior Connections are Successfully Opened, the request has been sent and the response is on  the way or it has already arrived.

 

[139.023] Stack: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
[139.023] ConnectionClosedException
[139.64] net_rim_os-3(4E211320)
[139.64]  ClientProtocol
[139.64]  handleIOExceptionBeforeResponse
[139.64]  0x11E1
[139.64] net_rim_os-3(4E211320)
[139.64]  ClientProtocol
[139.64]  writeFirstChunk
[139.929]  0x1511
[139.929] net_rim_os-3(4E211320)
[139.929]  ClientProtocol
[139.929]  writeRequest
[139.929]  0x7A5
[139.929] net_rim_os-3(4E211320)
[139.929]  ClientProtocol
[139.937]  flushOutputStream
[139.937]  0x10C1
[140.312]  x net_rim_os-3(4E211320)
[140.312]  StreamingHttpOutputStream
[140.312]  flush
[140.312]  0xB2E1

 

It seems the connection is closed before the request data is sent.

I am not manually closing any Output Stream until the data is sent.

Is there a chance that the OS tries to close such "hanged on server" connections? Is there a HTTP connection limit per Application?

 

I am not closing any connection until the received data is read from the stream.

 

 

*  When Using Edge/3G (Simulator)

I get an error when trying to open a connection ( Connector.open(url) ).

The error description is: "This tunnel is in NULLED_STATE"

It's the same scenario... Some of the connections are rapidly opened, request is sent / response is received / connection is closed.  Others throw this exception upon opening.

 

*  When Using Edge/3G (Device)

I cannot open not even 1 connection. I'm getting the "Tunnel Failed" exception.

 

I'm very confused about this behavior, the code for the HTTP connection procedures runs OK on OS 6 and OS 5.

 

Please advise If you have any idea why is this happening or what needs to be done to solve it.

I'll greatly appreciate some professional advise.

 

Best Regards,

Doru

 

 

 

 

 

 



 



 

 

 

 

 

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Blackberry OS 7 - Multiple HTTP Connections Failure

I have code that opens multiple connections in OS 7.0 (and below).  These are typically just http post and response commands, and short, so perhaps this is not a real comparison.  Currently the code has a limit of 3 or 4 connections at one time, which it does hit on both the Simulator and device.  And I've not seem a problem like this. 

 

How many Threads are you trying to run at once?

 

The only thing I can really comment on is this problem:

 

When Using Edge/3G (Device)

I cannot open not even 1 connection. I'm getting the "Tunnel Failed" exception.

 

Tunnel failed usually means that you have tried to open a Direct TCP (Carrier) connection and not specified the correct APN parameters.  What connection method are you using in this case?

 

Actually can you tell us in general about your Networking code?  Does it use ConnectionFactory?  or have you created something yourself? 

 

Please use plain text.
New Contributor
dorelberar
Posts: 3
Registered: ‎02-26-2009
My Device: Not Specified

Re: Blackberry OS 7 - Multiple HTTP Connections Failure

Hi Peter, and thank you for your quick reply.

 

About the threads:

I built custom Async Callback objects, that are stored(queued) within a vector. Two worker threads pull out the first callback from the queue and start processing it in another Processing Thread. Then, the Callback Object is removed from the waiting queue. 

 

I need this customized because i have requests that will wait and process some application settings for example and some of those threads will display a "Waiting" dialog box, until they are terminated.

 

About the HTTP Connections:

 

     * Opening HTTP connection

                  try {
                      hc = (HttpConnection) Connector.open(url);
                  } catch (Exception e) {

                       // Handle the exception here
                  }

     

       Does the URL need some APN parameter?

       I only add

           ";deviceside=true" if no wireless is available

                or

           ";deviceside=true;interface=wifi" if wireless is available

 

 

      * Sending the request

 

      // Setup HTTP Request to POST
      conn.setRequestMethod(HttpConnection.POST);
      conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
      conn.setRequestProperty("Accept_Language","en-US");
      //Content-Type is must to pass parameters in POST Request
      conn.setRequestProperty("Content-Type", "text/x-gwt-rpc; charset=utf-8");

 

      // build the actual string i want to send and set it to the URLEncodedPostData object
      postData = new URLEncodedPostData(null, false);
      postData.setData(jsonRequest.toJsonString(false));

     

      // set content lenth

      conn.setRequestProperty("Content-Length", "" + postData.getBytes().length);

       // write to output stream
      OutputStream os = conn.openOutputStream();
      os.write( postData.getBytes() );

      // close output stream
      os.flush();
      os.close();

 

      * Reading the response from the Input Stream

      I used IOUtilities.streamToBytes(inputStream) but with no success either.

      I turned back to the original code.

     

      try {

            // buffer for the response

            StringBuffer b = new StringBuffer();

 

            // input stream

            InputStream in = conn.openInputStream();

 

            // read character

           int ch=-1;

 

            // wait for available data on the stream - the response isn't available right away
            while((ch=in.read())==-1)
                Thread.sleep(20);

            b.append((char)ch);


            while( ( ch = in.read() ) != -1 ){
                b.append( (char) ch );
            }

          

            // notify response arrived here to process it ......

 

            // close input stream
           if( in != null){
                try {
                   in.close();
                }
               catch( IOException e ){
                  //   System.out.println(" EXCEPTION closing stream");
               }
          }


   } catch (Exception e) {
          // Input stream failed or closed
          // handle code here ......
   }



  Can you review this and make some suggestions? I have tried many things. I even used ConenctionFactory.

 

  Best regards,

  Doru

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Blackberry OS 7 - Multiple HTTP Connections Failure

Your network design, with worker Threads processing a request queue, seems fine to me.  So you are not flooding the device, which was my first concern.

 

To answer your questions:

 

1) "Does the URL need some APN parameter?

       I only add

           ";deviceside=true" if no wireless is available

                or

           ";deviceside=true;interface=wifi" if wireless is available"

 

Oh if it were only this simple.....

deviceside=true will only work in certain circumstances, typically when the user has already coded the APN information into the phone.  There are a number of other connection options.  Have a look here for the detail:

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

 

2)   I used IOUtilities.streamToBytes(inputStream) but with no success either.

 

In this situation, this method should be perfect.  I'd like to know why you had a problem.

 

3) while((ch=in.read())==-1)
                Thread.sleep(20);

 

This code should not be needed.  THe read should wait.  This looks like standard http processing to a standard server (is it, what is your Server running?  Apache? IIS?) and if you need this then there is something weird going on. 

 

4)
            while( ( ch = in.read() ) != -1 ){
                b.append( (char) ch );
            }

 

I hate this code with a passion, probably without justification.  data is NOT sent byte at a time, it will not turn up on the BB byte at a time and it doesn't need to be converted to characters - and that can introduce encoding issues.  I know there is a RIM sample that uses this approach, but that sample knows it is getting text.  Like I said above, IOUtiltiies should work, I would go back to using it. 

 

5)  I even used ConenctionFactory

 

I recommend that you go back to trying it.  It will give you better connectivity options (e.g. WAP) with no additional coding.

 

Hope this helps....

 

 

Please use plain text.
Regular Contributor
sreid
Posts: 69
Registered: ‎07-18-2011
My Device: 9630 Tour
My Carrier: Verizon

Re: Blackberry OS 7 - Multiple HTTP Connections Failure

Peter,

 

I seem to be having similar connectivity problems with OS 7, my code works on OS 5, but not on OS 7. In the connection URL I append ";deviceside=false" because we use BES.  In the simulator for 9900 the error is:

 

java.io.InterupptedIOException: Local connection timed-out after 12000

 

(i assume the error on the smartphone device is the same from what I've been told. Also in the simulator I have checked the box for MDS-CS)

 

 

here's a snippet of the connection code:

 

       
        //-----------//
        // POST data //
        //-----------//
        
        oCon = (HttpsConnection) Connector.open(url);
        oCon.setRequestMethod(HttpsConnection.POST);
        oCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        oCon.setRequestProperty("User-Agent", "BlackBerry/3.2.1");
        oCon.setRequestProperty("Content-Language", "en-US");
        oCon.setRequestProperty("Connection", "close");
        oCon.setRequestProperty("Content-Length", Integer.toString(oPostData.size()));
 
        strmOut = oCon.openOutputStream();
        strmOut.write(oPostData.getBytes());
 
        strmOut.flush();
 
        
        inputStream = oCon.openInputStream();
        ret = processServerResponse(oCon, inputStream);
 
        // Clean up
        if (inputStream != null)
            inputStream.close();
        if (oCon != null)
            oCon.close();        
        if (strmOut != null)
            strmOut.close();   
 

thanks for any advice,

Steve

Please use plain text.