1

Android 的设计不允许主线程上的网络,它可以被强制,但这不是一个好习惯。所以我想同步主线程和另一个线程,这样我就可以从数据库中获取响应以进行用户身份验证。我的同步不起作用..

这是主线程中的代码..

if (loginBTN.getId() == ((Button) v).getId()) {

    String emailId = loginField.getText().toString().trim();
    String password = passwordField.getText().toString().trim();

    postParameters.add(new BasicNameValuePair("username", emailId));
    postParameters.add(new BasicNameValuePair("password", password));

    try {

        System.out.println("b4 response" + responseToString);

        checkLogin();

        System.out.println("after response" + responseToString);

        synchronized (this) {

            while (isAvailable == false) {

                wait();

            }

            if (responseToString.equalsIgnoreCase("1")) {

                statusLBL.setText("Login Successful...");

            }

            else {

                statusLBL.setText("Sorry Incorrect Username or Password...");

            }
        }

    }

    catch (Exception e) {

        statusLBL.setText(e.toString());
    }

}




private void checkLogin() {

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {

                try {

                    HttpClient http = new DefaultHttpClient();
                    HttpPost request = new HttpPost("http://example.org/api/android_gradhub/login_user.php");
                    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
                    request.setEntity(formEntity);
                    HttpResponse response = http.execute(request);
                    HttpEntity entity = response.getEntity();

                    // responseToString = EntityUtils.toString(entity);

                    synchronized (this) {

                        while (isAvailable == true) {

                            wait();

                        }

                        responseToString = EntityUtils.toString(entity);
                        notifyAll();
                    }

                    System.out.println("response " + responseToString);
                }

                catch (Exception e) {

                    e.printStackTrace();
                }
            }
        });

        thread.start();

    }
4

1 回答 1

0

Remove all of your synhronization and then just put thread.join() as the last statement in checkLogin. This is the simplest way to wait for the thread to complete before proceeding.

If you want to decide when to wait (like you might want to do other stuff in the main program before the login needs to be checked) then you can return the thread from checkLogin. Than you decide when to join back in your main program. (i.e. signature changes to public Thread checkLogin and last line of checkLogin becomes return thread. Back in your main program, call Thread thread = checkLogin(); when you want to wait for the checkLogin to finish - use thread.join(), up until that point Main thread and checkLogin thread is concurrent).

于 2013-05-26T01:00:57.343 回答