在android中使用Asyn-task帮助登录Activity

时间:2013-09-24 13:10:16

标签: java android multithreading android-layout android-asynctask

我的问题是在执行此任务时显示警告消息请发布正确的背景过程代码。

public class loginTask extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... urls) {


        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username", editTextUsername.getText().toString()));
        postParameters.add(new BasicNameValuePair("password", editTextPassword.getText().toString()));
        String response = null;String res=null;
        try {
            System.out.println("Called for login server");
            response = CustomHttpClient.executeHttpPost("login.php", postParameters);
            System.out.println("connect");
            res=response.toString();
            res= res.replaceAll("\\s+","");


            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return res;


    }
    protected void onPostExecute(String result) {
        if(res.equals("success"))
            {
                Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
                startActivity(mainintent);
                finish();
                //error.setText("Correct Username or Password");
            }
            else
            {
                /*Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
                editTextUsername.setText("");
                editTextPassword.setText("");
                editTextUsername.requestFocus();*/
                error.setText("Sorry!! Incorrect Username or Password");
        pDialog.dismiss();
    }
}

logcat的

09-24 09:07:54.894: W/System.err(4212): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-24 09:07:54.946: W/System.err(4212):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
09-24 09:07:54.954: W/System.err(4212):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
09-24 09:07:54.954: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.964: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.964: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.984: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.984: W/System.err(4212):     at android.widget.ScrollView.requestLayout(ScrollView.java:1440)
09-24 09:07:54.993: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:54.993: W/System.err(4212):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:358)
09-24 09:07:55.014: W/System.err(4212):     at android.view.View.requestLayout(View.java:15792)
09-24 09:07:55.014: W/System.err(4212):     at android.widget.TextView.checkForRelayout(TextView.java:6524)
09-24 09:07:55.034: W/System.err(4212):     at android.widget.TextView.setText(TextView.java:3771)
09-24 09:07:55.034: W/System.err(4212):     at android.widget.TextView.setText(TextView.java:3629)
09-24 09:07:55.054: W/System.err(4212):     at android.widget.TextView.setText(TextView.java:3604)
09-24 09:07:55.054: W/System.err(4212):     at com.loginpack.LoginActivity$loginTask.doInBackground(LoginActivity.java:111)
09-24 09:07:55.064: W/System.err(4212):     at com.loginpack.LoginActivity$loginTask.doInBackground(LoginActivity.java:1)
09-24 09:07:55.084: W/System.err(4212):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-24 09:07:55.084: W/System.err(4212):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-24 09:07:55.104: W/System.err(4212):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-24 09:07:55.114: W/System.err(4212):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-24 09:07:55.134: W/System.err(4212):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-24 09:07:55.154: W/System.err(4212):     at java.lang.Thread.run(Thread.java:841)

执行后,它移动到另一个活动,但用户名和密码错误,它没有显示在我的屏幕上点击的UI后面只显示错误登录消息

5 个答案:

答案 0 :(得分:0)

您需要将更新用户界面的任何代码(例如error.setText)移至onPostExecute内。

答案 1 :(得分:0)

       Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
            startActivity(mainintent);
            finish();

此代码应位于onPostExecute()方法中。

答案 2 :(得分:0)

doInBackground() 是一个后台非UI线程,因此无法访问应用程序中定义的布局。因此,任何与UI无关的代码都不能写入其中。

另一方面, onPostExecute() 是一个UI线程,通常用于在AsyncTask完成后对UI进行更改。

您需要做什么:

在AsyncTask类中定义一个标志,并在doInBackground()内使用它来设置它是否成功。然后,在onPostEXecute()内,检查标志的值并显示相应的消息。

在课程定义中添加 boolean successFlag

doInBackground()

中设置标记的值
doInBackground()
{
...

            if(res.equals("success"))
            {
             successFlag = true;
            }
            else
            {
             successFlag = false;
            }

...

}

onPostExecute() 中检查此标记的值。

onPostExecute()
{
     if(successFlag)
{
                Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
                startActivity(mainintent);
                finish();
}
      else
{
                error.setText("Sorry!! Incorrect Username or Password");
}
}

答案 3 :(得分:0)

你必须改变这个..

protected void onPostExecute(String result) {

     if(result.equals("success"))
        {
            Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
            startActivity(mainintent);
            finish();
            //error.setText("Correct Username or Password");
        }
        else
        {
            /*Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
            editTextUsername.setText("");
            editTextPassword.setText("");
            editTextUsername.requestFocus();*/
            error.setText("Sorry!! Incorrect Username or Password");

        }


    pDialog.dismiss();
}

这是因为您无法更新UI doInBackground()方法..您应该使用 onPostExecute()方法..

答案 4 :(得分:0)

根据if / else条件在doInBackground true / false中设置一个布尔变量,并在onPostExecute中检查以启动新的Intent或显示错误消息

boolean isSuccess=false;

doInBackground(){
   if(res.equals("success"))
            {
              //do your work
              isSuccess=true;
            }
            else
            {
              //do your work
              isSuccess=false;

            }

}

在onPostExecute

protected void onPostExecute(String result) {

 if(isSuccess)
    {
        Intent mainintent=new Intent(LoginActivity.this, MainActivity.class);
        startActivity(mainintent);
        finish();

    }
    else
    {
        /*Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
        editTextUsername.setText("");
        editTextPassword.setText("");
        editTextUsername.requestFocus();*/
        error.setText("Sorry!! Incorrect Username or Password");

    }


pDialog.dismiss();
}