Sending an HTTP request in a separate thread is a very common operation for almost every client-server web application. The idea is to spin-off a background thread that will send a request and process the response data, but won’t block the main (GUI) thread on the same time. At the end of its execution the background thread should use some kind of a callback mechanism to pass response results back to the main thread. Since this operation is very common I was expecting that pretty much every modern framework should have a lot of tutorials available online describing how to implement this functionality in a quick and easy way. However this was not the case for the BlackBerry Java API - after a couple of hours researching I still didn’t find anything that was ‘quick and easy’…
Finally, looking through RIM’s API docs I found a method Application.invokeLater() which can take a runnable object defined in a background thread and start it in the main thread. This method allowed me to pass execution control back to the main thread.
Here is a sample code:
First let’s create a ConnectionThread class that can take a URL (to send a request to) and call a method of some screen once the response is received (for the sake of simplicity we’ll be calling a method of the screen that is currently on top of the screen’s stack).
public class ConnectionThread extends Thread { String URL; public ConnectionThread(String URL) { this.URL = URL; //URL to send a request to } public void run() { //optional: show some popup "Please wait screen" final Screen dialogScreen = new WaitPopupScreen(); //wait popup screen extends RIM's PopupScreen class UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().pushModalScreen(dialogScreen); } }); //send HTTP request and save the response HttpConnection connection = null; //use API 5.0 Connection factory class to get first available connection byte responseData[] = null; try { connection = (HttpConnection) new ConnectionFactory().getConnection(URL).getConnection(); int len = (int) connection.getLength(); responseData = new byte[len]; DataInputStream dis; dis = new DataInputStream(connection.openInputStream()); dis.readFully(responseData); } catch (Exception e) { // TODO Handle exception } final byte[] responseDataToProcess = responseData; //use invokeLater method to pass results back to the main thread UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().popScreen(dialogScreen); //hide wait popup screen //pass results to the callback mathod of the current screen ((MyScreen)UiApplication.getUiApplication().getActiveScreen()).callBackMethod(responseDataToProcess); } }); } }This is the actual screen where we spin the connection thread off:
public class MyScreen extends MainScreen { public MyScreen() { super(); // add a button that will initiate an HTTP request ButtonField requestButton = new ButtonField("Send HTTP Request"); requestButton.setChangeListener(new FieldChangeListener() { public void fieldChanged(Field field, int context) { //start connectionThread on a button click new ConnectionThread("http://mnarinsky.com").start(); } }); add(requestButton); } //this method will be called from the connection thread public void callBackMethod(byte[] responseData){ //process response } }Finally this is a code for an optional 'Please wait' popup screen – displays a simple “Please wait..” text. If you want you can make it little more fancy by using some animated GIF image.
public class WaitPopupScreen extends PopupScreen { public WaitPopupScreen() { super(new VerticalFieldManager()); LabelField labelField = new LabelField("Please wait...", Field.FIELD_HCENTER); add(labelField); } }