简体   繁体   中英

Blackberry multi threading issue

I am developping a BlackBerry application which communicates with the server via HTTP requests( javax.microedition.io.HttpConnection ). On device, user clicks some UI items, and device sends the requests to server, when the response comes, UI changes. Communication takes place under new thread, while UI thread pushes and pops ProgressDialogScreen.

The problem is sometimes, when response comes and ProgressDialogScreen is popped, UI does not change but after couple seconds UI changes. If you have requested in between when ProgressDialogScreen is popped and when new Screen is pushed, there comes the mess. First oldest new Screen is pushed, and the newest new Screen is pushed. And this situation can be observed like server responsing wrong requests. This problems occur on simulator and device.

The other problem is, sometimes two same response returns for one request. I was able to see these two problems on simulator at the logs, but i have not able to see this issue on device since i can not see the logs.

EDIT:

String utf8Response;
HttpConnection httpConn = null;
try{
    httpConn = (HttpConnection) Connector.open(url);
    httpConn.setRequestMethod(HttpConnection.GET);
    httpConn.setRequestProperty("Content-Type", "text/html; charset=UTF8");
    if(sessionIdCookie != null){
        //may throw IOException, if the connection is in the connected state.
        httpConn.setRequestProperty("Cookie", sessionIdCookie);
    }
}catch (Exception e) {
    //...
}

try{
    httpConn.getResponseCode();
    return httpConn;
}catch (IOException e) {
    // ...
}
byte[] responseStr = new byte[(int)httpConn.getLength()];
DataInputStream strm = httpConn.openDataInputStream();
strm.readFully(responseStr);
try{
    strm.close();
}catch (IOException e) {
    // ....
}
utf8Response = new String(responseStr, "UTF-8");

If this code successfully run, this piece of code runs and new screen is pushed:

UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
       Vector accounts = Parser.parse(utf8Response,Parser.ACCOUNTS);
       if (accounts.size() == 0){
           DialogBox.inform(Account.NO_DEPOSIT);
           return;
       }
       currentScreen = new AccountListScreen(accounts);
       changeScreen(null,currentScreen);
    }
});

public void changeScreen(final AbstractScreen currentScreen,final AbstractScreen nextScreen) {
    if (currentScreen != null) 
        UiApplication.getUiApplication().popScreen(currentScreen);
    if (nextScreen != null)
        UiApplication.getUiApplication().pushScreen(nextScreen);
}

EDITv2:

private static void progress(final Stoppable runThis, String text,boolean cancelable) {
    progress = new ProgressBar(runThis, text,cancelable);
    Thread threadToRun = new Thread() {
        public void run() {
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    try{
                        UiApplication.getUiApplication().pushScreen(progress);
                    }catch(Exception e){
                        Logger.log(e);
                    }
                }
            });
            try {
                runThis.run();
            } catch (Throwable t) {
                t.printStackTrace();
            }
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                 public void run() {
                    try {
                        UiApplication.getUiApplication().popScreen(progress);
                    } catch (Exception e) { }
                 }
            });
        }
   };
   threadToRun.start();
}

By the way ProgressBar is extended from net.rim.device.api.ui.container.PopupScreen and Stoppable is extended from Runnable

I preferred to pop progress bar after new Screen is prepared and pushed. This way there will be no new request between request and response.

Why not do:

private static void progress(final Stoppable runThis, String text,boolean cancelable) {
    progress = new ProgressBar(runThis, text,cancelable);
    UiApplication.getUiApplication().pushScreen(progress);
    [...]

Seems like you are parsing on the UI Thread. Please remove Vector accounts = Parser.parse(utf8Response,Parser.ACCOUNTS); from ui thread and do it in a separate thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM