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 Developer
Posts: 4
Registered: ‎06-23-2009
My Device: Not Specified
Accepted Solution

HTTP Connection's getResponseCode failing? :(

Hey all,

 

I'm fairly new to BB development.  I am trying to get my app to connect to the web and pull down some data.  I think I'm close, but after using the debugger and stepping through my code, I notice it stops at the "rc = c.getResponseCode();" line below.  (By stops I mean it executes it... doesn't give any errors, but it never steps onto the next line.... everythign just sits there... )

 

What am I missing?

 

System.out.println(">>> in new threads run"); try { HttpConnection c = null; InputStream is = null; int rc; try { System.out.println(">>> in new threads try"); c = (HttpConnection)Connector.open("http://www.google.ca"); System.out.println(">>> c : "+c); // 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(); System.out.println(">>> rc : "+rc); //testing(rc); //String ryan = Integer.toString(rc); //Bitmap icon = Bitmap.getBitmapResource("img/download.png"); //statusDlg = new Dialog(ryan,null,null,0,icon); //statusDlg.show(); if (rc != HttpConnection.HTTP_OK) { throw new IOException("HTTP response code: " + rc); } is = c.openInputStream(); System.out.println(">>> is : "+is); // Get the ContentType String type = c.getType(); System.out.println(">>> type : "+type); // Get the length and process the data int len = (int)c.getLength(); System.out.println(">>> len : "+len); 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) { System.out.println(">>> WTF : "+e); throw new IllegalArgumentException("Not an HTTP URL"); } finally { System.out.println(">>> WTF2"); if (is != null) is.close(); if (c != null) c.close(); } } catch (IOException e) { } catch (Exception e) { e.printStackTrace(); }

 

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

Re: HTTP Connection's getResponseCode failing? :(

It may be waiting for the connection to complete. Network connection attempts take a while to timeout -- on the order of minutes. If you're debugging in the simulator, have you fired up the MDS simulator? If on a device, is it connected?

 

You also might want to take a look at the CoverageInfo class as a way of checking connection availability before actually attempting a connection.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Developer
Posts: 4
Registered: ‎06-23-2009
My Device: Not Specified

Re: HTTP Connection's getResponseCode failing? :(

Oh, I didn't fire up any MDS simulator.... does that come with the BB JDE?  Or soemthign seperate I need to download? 

 

I'll start googling....

New Developer
Posts: 4
Registered: ‎06-23-2009
My Device: Not Specified

Re: HTTP Connection's getResponseCode failing? :(

Thankyou.... I didn;t have the MDS simulator running.  All works now Smiley Happy
Regular Visitor
Posts: 1
Registered: ‎09-05-2011
My Device: 5800 simulator
My Carrier: blackberry app developer

Re: HTTP Connection's getResponseCode failing? :(

Dear friend I have the same problem as u had. 

I have downloaded the mentioned simulator from the same site and stored it in same folder where my blackberry plugins are stored. but still same problem persist.