Search code examples
javamultithreadingblackberryhttpconnection

Blackberry application cant be opened in simulator


everyone. I'm new here and this is my first post. I'm creating a simple app that takes a content of the web. Below here is my code. The problem is, I cant get to run this on the simulator. No errors, no dialog boxes, just completely cannot be opened. If there's anybody who can help me....

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class HTTPClient extends UiApplication {
LabelField test;

MainScreen screen = new MainScreen();

public static void main(String[] args)
{
HTTPClient theApp = new HTTPClient();
theApp.enterEventDispatcher();
}

public HTTPClient()
{
getPage("http://google.com");
}

public void getPage(String url) {
String response = "";
try {
StreamConnection s = (StreamConnection)Connector.open(url);
InputStream input = s.openInputStream();
byte[] data = new byte[256];
int len = 0;
StringBuffer raw = new StringBuffer();
while( -1 != (len = input.read(data))) {
raw.append(new String(data, 0, len));
}
response = raw.toString();
show(response);

input.close();
s.close();
} catch(Exception e) { }
}

public void show(String response) {
test = new LabelField(response);
screen.add(test);
pushScreen(screen);
}
}

Solution

  • Two notes regarding the code you posted:

    1. Networking operations (to be more precise, I would say that all non UI operations) should be done in a separate worker thread and not in the main event thread (UI thread in case of UIApplication).
    2. If you need access to the UI from outside the UI thread, then you can use the Application's invokeLater() or invokeAndWait() methods. Alternatively, a worker thread can synchronize on the event lock (returned by Application.getEventLock()) to ensure serialized access to the UI. Note that you should only hold this lock for short periods of time.

    As for the BlackBerry simulator and HTTP - In order to test a BlackBerry application that uses HTTP connection with the BlackBerry simulator, one must use the BlackBerry MDS (Mobile Data System) connection service. Here is a link to the relevant guide.

    Start the BlackBerry MDS Connection Service when you start the BlackBerry Smartphone Simulator

    1. In Eclipse®, on the Run menu, click Debug Configurations or Run Configurations.
    2. Expand the BlackBerry Simulator item.
    3. Complete one of the following tasks:
      • To work with an existing launch configuration, under BlackBerry Simulator, click a launch configuration.
      • To create a new launch configuration, right-click BlackBerry Simulator, select New.
    4. Click the Simulator tab.
    5. Click the General tab.
    6. Select the Launch Mobile Data System Connection Service (MDS-CS) with simulator check box.
    7. Click Apply.

    I also highly recommend to you to check the HTTPDemo sample that comes with the JREs you have downloaded (you have installed at least one JRE if you are able to compile your code). Here is guide on how to import these samples into Eclipse plugin.

    As for your code, I've modified it to meet the requirements I've mentioned:

    import java.io.InputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.container.MainScreen;
    
    public class HTTPClient extends UiApplication {
        private LabelField labelField;
    
        public static void main(String[] args) {
            HTTPClient theApp = new HTTPClient();
            theApp.enterEventDispatcher();
        }
    
        public HTTPClient() {
            MainScreen httpScreen = new MainScreen();
            labelField = new LabelField();
            httpScreen.add( labelField);
            pushScreen(httpScreen);
    
            new Thread() {
                public void run() {
                    getPage("http://google.com");
                }
            }.start();
        }
    
        public void getPage(String url) {
            try {
                StreamConnection s = (StreamConnection) Connector.open(url);
                InputStream input = s.openInputStream();
                byte[] data = new byte[256];
                int len = 0;
                StringBuffer raw = new StringBuffer();
                while (-1 != (len = input.read(data))) {
                    raw.append(new String(data, 0, len));
                }
                input.close();
                s.close();
    
                show(raw.toString());
    
            } catch (Exception e) {
            }
        }
    
        public void show(final String response) {
            Thread t = new Thread() {
                public void run() {
                    labelField.setText(response);
                }
            };
            UiApplication.getUiApplication().invokeLater(t);
        }
    }