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
Developer
Posts: 342
Registered: ‎09-28-2009
My Device: Not Specified

HTTPConnection Failure

Howdy, all

 

I'm trying to run some code on some actual devices, and it fails, even though it works in the emulator.  Is there some other way of making connections that is preferable?

 

String szReturn = "Error";
        DataInputStream iStrm = null;
        ByteArrayOutputStream bStrm = null;
        try
        {
            connection = (HttpConnection)Connector.open(szStatement, 1, true);
            connection.setRequestMethod("GET");
            iStrm = connection.openDataInputStream();
            byte rawData [];
            int length = (int) connection.getLength();
            if (length != -1)
            {
                rawData = new byte [length];
                iStrm.readFully(rawData);
                szReturn = iStrm.toString();
            }
            else
            {
                bStrm = new ByteArrayOutputStream();
                int ch;
                while((ch = iStrm.read()) != -1)
                    bStrm.write(ch);
                rawData = bStrm.toByteArray();
                bStrm.close();
                szReturn = bStrm.toString();
            }
        }
        catch(Exception e)
        {
            szError = "JNetwork2.java - ExecuteStatementHTTP() - Unable to connect to network. Please check your wireless settings and try again.";
            bFailure = true; System.out.println(szError);
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(iStrm != null)
                    iStrm.close();
                if(connection != null)
                    connection.close();
                if(bStrm != null)
                    bStrm.close();
            }
            catch(Exception e)
            {
                szError = "JNetwork2.java - ExecuteStatementHTTP() - Unable to close network connection.";
                bFailure = true; System.out.println(szError);
                e.printStackTrace();
            }
        }
        return szReturn;

 

Thanks.

Joseph

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: HTTPConnection Failure

The simulator emulates a BES environment (with MDS-CS running). You code will not run on most devices that are not on a BES, unless you add connection parameters.

 

I hate to keep kicking a dead horse, but this ground is covered daily on the forum. There is also a sticky thread at the top of the forum that addresses this topic specifically.

 

You will need to determine what type of transport is available: see CoverageInfo.

 

You will need to specify connection parameters to make that transport work:

 

Read this:

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is...

 

And you will likely have to provide APN info for your carrier.

 

Read this:

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_...

 

 

Developer
Posts: 342
Registered: ‎09-28-2009
My Device: Not Specified

Re: HTTPConnection Failure

Not a helpful response.  Of course I've tried many more solutions than what I posted before asking here.  This is a problem that extends beyond BlackBerry as well, being part of the Java implmentation.

Developer
Posts: 77
Registered: ‎01-27-2010
My Device: Bold 9000
My Carrier: AT&T

Re: HTTPConnection Failure

 


GoldenJoe wrote:

Not a helpful response.  Of course I've tried many more solutions than what I posted before asking here.  This is a problem that extends beyond BlackBerry as well, being part of the Java implmentation.


 

 

Well unless you give us some idea of what you've tried it's not a helpful question.  I'm just going to restate what RexDoug said but just from glancing at your code it seems as if you're not connecting correctly (specifically it would be helpful if we knew what is in your URL variable).  I haven't programmed for other J2ME devices but I suspect that the solution to this problem will be BlackBerry specific.  When connecting to a network resource on BlackBerry you almost always need to supply extra information after the URL e.g. if you want to connect over DirectTCP and your device has the correct information stored you can append ";deviceside=true" to your URL.  So for instance

 

Connector.open("http://www.google.com;deviceside=true");

Different connection methods require different things to be appended to the URL.  Review the thread from peter_strange and the two links that RexDoug provided you.  If you're still having problems give us some indication that you've at least tried to figure things out on your own first.

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: HTTPConnection Failure

I didn't get this fro your original post:

 

This is a problem that extends beyond BlackBerry as well, being part of the Java implmentation.

 

..since you said that the code works on the simulator.

 

I guess you'll need to be more specific about the issue, the specific exception, what device, what carrier, what OS level on the device, what OS level your compiler is targeting, etc.

 

Highlighted
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: HTTPConnection Failure

I found the RIM videos Network Transports and, in particular, Networking Transports II to be very helpful. (The first one is a little too dense as an introduction, in my opinion.) The second video, in particular, has a nice prioritization of connection methods for both consumer and enterprise applications. Afterwards, you might also want to take a look at the Network Diagnostic Tool (which, besides being what its name says, is also great sample code for how to make network connections).




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.