title: login to http server from J2ME app
body: <p>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 <code>thread</code>.</p>
  
  <p><strong>Here is my question:</strong>
  I need the app to wait for the response from the server which in another <code>thread</code> and do the rest of code.</p>
  
  <p>This is login Midlet</p>
  
  <pre><code>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;
      }
  </code></pre>
  
  <p>The <code>Thread</code> 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.</p>
  
  <p>Any one help me to make the execution wait for thread until it finish. i tried <code>join()</code> but code end with hang up.</p>