2

I'm trying to test an http connection from my blackberry simulator but nothing happens. Do I need an MDS simulator for running this App? I am using Eclipse Helios BlackBerry Java Plug-in 1.5.0. I've also installed blackberry JDE in my system.

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

        try {
            c = (HttpConnection)Connector.open(url);
            RichTextField rt = new RichTextField("TEST LENGTH");
            // 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) {
                rt = new RichTextField("TEST LENGTH"+len);
                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 {
                rt = new RichTextField("GREATER LENGTH"+len);
                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();
        }
    }

Help is highly appreciated, Thanks, VKS

javanna
  • 59,145
  • 14
  • 144
  • 125
vks
  • 6,649
  • 7
  • 36
  • 55
  • 2
    Out of curiosity, how are you actually detecting if this was successful? From looking at it there isn't really anything that would indicate that you actually got the string. If it isn't throwing any Exceptions, it very well could be working. – jprofitt Dec 08 '11 at 13:12
  • but i added 3 RichTextFields in 3 conditions,it is not found in screen? – vks Dec 09 '11 at 12:47
  • You're creating new `RichTextField`s but you aren't actually adding them to the screen. – jprofitt Dec 09 '11 at 12:54

2 Answers2

4

You need to run the MDS-CS simulator to connect to the internet using a blackberry simulator

javanna
  • 59,145
  • 14
  • 144
  • 125
rfsk2010
  • 8,571
  • 4
  • 32
  • 46
1

No, you don't need to use MDS-CS.

You can configure the simulator to use your PC's normal network connection (bypassing MDS-CS) by going to Manage Connections->Set Up Wi-Fi Network and connecting to "Default WLAN Network".

donturner
  • 17,867
  • 8
  • 59
  • 81