简体   繁体   中英

login to http server from J2ME app

I have a login Midlet contains text field and password fields. then i have two action commands to login or cancel. When user press login, the app ask to activate the internet connection and make the HTTP request in another thread .

Here is my question: I need the app to wait for the response from the server which in another thread and do the rest of code.

This is login Midlet

public class MyloginMedlet extends MIDlet implements CommandListiner {
private TextField username;
private TextField password;
public MyloginMedlet() {
// steps required to create the login ui here
}
public void startApp() {
// app starts here and disply the ui elements
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
    notifyDestroyed();
}
/*
* Here things gets intersting
*/
public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    if (label.equals("Cancel") || label.equals("Exit")) {
        destroyApp(true);
    } else if (label.equals("Login")) {
        Thread t = new Thread() {
            public void run() {
             serverResponse = connect(userName.getString(), password.getString());
            }
          };
          t.start();
          if (serverResponse == "isValidUser") {
                // Go to app dashboard
         } else {
                // Alert User with failed login
          }
    }
//
// Conect through HTTP
private String connect(String name, String password2) {
    String url = "http://192.168.1.90/checkUser.php";
    // String url = getAppProperty("NetworkThreading.URL");

    try {
        // Query the server and retrieve the response.
        HttpConnection hc = (HttpConnection) Connector.open(url);
        InputStream in = hc.openInputStream();

        // Pull back the server's response. If a content length is not
        // specified, we'll just read 255 bytes.
        int contentLength = (int) hc.getLength();
        if (contentLength == -1)
            contentLength = 255;
        byte[] raw = new byte[contentLength];
        int length = in.read(raw);

        // Clean up.
        in.close();
        hc.close();

        // Show the response to the user.
        String s = new String(raw, 0, length);
        System.out.println(s);
        return s;
//          Alert a = new Alert("Response", s, null, null);
//          a.setTimeout(Alert.FOREVER);
//          mainScreen.setCurrent(a);
        } catch (IOException ioe) {
            Alert a = new Alert("Exception", ioe.toString(), null, null);
            a.setTimeout(Alert.FOREVER);
            // mDisplay.setCurrent(a, mMainForm);
        }
        return null;
    }

The Thread isn't wait for HTTP request to finish. it always go to alert area (commented area). also i tried to put the check code in the thread but no succeed.

Any one help me to make the execution wait for thread until it finish. i tried join() but code end with hang up.

finally i find a solution on how to solve this:

  • When you call run() method, you should create a form (Screen) to inform user to wait until thread finish it's work.

Form f = new Form("Please Waite..."); f.append("Please Waite..."); Display.getDisplay(this).setCurrent(f);

  • Put this code in commandAction method before you call run() method.

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