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);
}
}
Two notes regarding the code you posted:
UIApplication).Application‘sinvokeLater()orinvokeAndWait()methods. Alternatively, a worker thread can synchronize on the event lock (returned byApplication.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.
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: