无效登录的Toast没有显示? Android的

时间:2015-01-01 13:59:04

标签: java android

我正在使用android创建一个登录应用程序。我试图显示一个对话框,只要我输入无效的凭据,但它不起作用?我究竟做错了什么? 虽然我只需要错误吐司,但我能够正常登录

代码段

btnLogin.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog = ProgressDialog.show(MainActivity.this, "",
                        "Validating your Account", true);
                new Thread(new Runnable() {
                    public void run() {
                        login();
                    }
                }).start();
            }
        });
    }

    void login(){
        try{

            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://10.0.3.2/sunshine-ems/login.php"); // make sure the url is correct.
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
            nameValuePairs.add(new BasicNameValuePair("username",inputUsername.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("password",inputPassword.getText().toString().trim()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            response=httpclient.execute(httppost);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);

            String username = inputUsername.getText().toString().trim();
            switch(Integer.parseInt(response)){
                case 0:

                        session.createLoginSession(username);
                        Intent b = new Intent(MainActivity.this, Profile.class);
                        startActivity(b);
                        finish();
                    break;
                default:


                            Toast.makeText(MainActivity.this,"Invalid username or password.", Toast.LENGTH_LONG).show();


                    break;
            }
            dialog.dismiss();

        }catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
    }
}

Logcat表示无效输入

01-01 14:16:37.179: W/audio_hw_primary(111): out_write() limiting sleep time 32244 to 23219
01-01 14:16:47.699: W/audio_hw_primary(111): out_write() limiting sleep time 44149 to 23219
01-01 14:16:47.711: W/SingleClientConnManager(1571): Invalid use of SingleClientConnManager: connection still allocated.
01-01 14:16:47.711: W/SingleClientConnManager(1571): Make sure to release the connection before allocating another one.
01-01 14:16:47.723: W/EGL_emulation(1571): eglSurfaceAttrib not implemented
01-01 14:16:47.723: W/audio_hw_primary(111): out_write() limiting sleep time 55759 to 23219
01-01 14:16:47.747: I/System.out(1571): Exception : Invalid int: ""
01-01 14:16:47.755: W/audio_hw_primary(111): out_write() limiting sleep time 27369 to 23219
01-01 14:16:47.927: W/InputMethodManagerService(472): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@53241ec4 attribute=null, token = android.os.BinderProxy@533337e4
01-01 14:16:47.931: W/audio_hw_primary(111): out_write() limiting sleep time 25917 to 23219

2 个答案:

答案 0 :(得分:0)

您正尝试从后台主题更新用户界面

使用AsyncTask。检查doInBackground()中的登录详细信息。在Toast/AlertDialogue中显示onPostExecute()

Here是了解AsyncTask

用法的一个很好的例子

答案 1 :(得分:-1)

将此添加到您的活动

    private class Login extends AsyncTask<Void,Void,Void>{
    private int result=9;
    @Override
    protected Void doInBackground(Void... params) {

        try{

            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://10.0.3.2/sunshine-ems/login.php"); // make sure the url is correct.
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
            nameValuePairs.add(new BasicNameValuePair("username",inputUsername.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("password",inputPassword.getText().toString().trim()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            response=httpclient.execute(httppost);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);
            result=Integer.parseInt(response);
        return null;
        }catch (Exception e){
            System.out.println("Exception : " + e.getMessage());
            return null;
        }

    }
    @Override
    protected onPostExecute(){
        switch(Integer.parseInt(response)){
            case 0:
                session.createLoginSession(username);
                Intent b = new Intent(MainActivity.this, Profile.class);
                startActivity(b);
                finish();
                break;
            default:
                Toast.makeText(MainActivity.this,"Invalid username or password.", Toast.LENGTH_LONG).show();
                break;
        }
    }

}

这是给你onResumeonCreate

 btnLogin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            dialog = ProgressDialog.show(MainActivity.this, "",
                    "Validating your Account", true);
            new Login().execute()

        }
    });

并阅读官方文档中的AsyncTasktutorial