mercredi 3 juin 2015

else inside if condition not checking in android

Hi in the below code else inside if condition inside is not checking if the username and password matches with Admin user found directly it's checking only if after that else if was checking.But inside else under the if condition was not going.

java

public class Login extends Activity {
    ImageButton login;
    TextView tv1,tv,tv2;
    Button back,home;
    private static final Pattern USERNAME_PATTERN = Pattern
            .compile("[a-zA-Z0-9]{1,250}");
    private static final Pattern PASSWORD_PATTERN = Pattern
            .compile("[a-zA-Z0-9+_.]{4,16}");
    EditText usname,pword,ustype;

    Boolean isInternetPresent = false;
    String username,password;
    HttpPost httppost;
    StringBuffer buffer;
    String queryString;
    String data="";
    int i;
    HttpResponse response;
    HttpClient httpclient;
    CheckBox mCbShowPwd;
    SessionManager session;
     private ProgressDialog progressDialog; 

     ConnectionDetector cd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        tv1=(TextView) findViewById(R.id.tv);

        back=(Button) findViewById(R.id.back);
        home=(Button) findViewById(R.id.home);
        tv1.setText("Login");
       // session = new SessionManager(getApplicationContext());
       // session.checkLogin();


       // final HashMap<String, String> user = session.getUserDetails();



        login = (ImageButton)findViewById(R.id.login);  

        usname = (EditText)findViewById(R.id.username);
        pword= (EditText)findViewById(R.id.password);
        ustype= (EditText)findViewById(R.id.usertype);

        tv1 = (TextView)findViewById(R.id.tv2);

        mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);
        cd = new ConnectionDetector(getApplicationContext());
       mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (!isChecked) {

                    pword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {

                     pword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });


       login.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {

               new LoadViewTask().execute(); 



               isInternetPresent = cd.isConnectingToInternet();
               if (!isInternetPresent) {
                   showAlertDialog(Login.this, "No Internet Connection",
                           "You don't have internet connection.", true);

                   return;


               }

               String username = usname.getText().toString();
               String password = pword.getText().toString();

              // String name = user.get(SessionManager.KEY_USERNAME);


                   if (username.equals("")) {
                       Toast.makeText(Login.this, "ENTER USERNAME",
                               Toast.LENGTH_LONG).show();

                   }
                   if (password.equals("")) {
                       Toast.makeText(Login.this, "ENTER PASSWORD",
                               Toast.LENGTH_LONG).show();

                   }

              else if (!CheckUsername(username) && !CheckPassword(password)){
                       Toast.makeText(Login.this, "ENTER VALID USERNAME & PASSWORD",
                             Toast.LENGTH_LONG).show();
               }
               else{
                   queryString = "username=" + username + "&password="
                            + password ;
                  String usertype = DatabaseUtility.executeQueryPhp("login",queryString);
                  System.out.print(usertype);

            if(usertype.equalsIgnoreCase("Admin user Found")){
                runOnUiThread(new Runnable() {
                    public void run() {

                        Toast.makeText(Login.this, "Login Sucess",
                                   Toast.LENGTH_LONG).show();


                    }
                });


                Intent in=new Intent(Login.this, Reports.class);
                startActivity(in);

            }



            else if(usertype.equalsIgnoreCase("No User Found")){
                runOnUiThread(new Runnable() {
                    public void run() {


                         tv2.setText("InValid UserName and Password");

                    }

                });
                        }


            }

           }


         });
       back.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                onBackPressed();

            }
        });
home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent in=new Intent(getApplicationContext(),MainActivity.class);
                startActivity(in);

            }
        });
   }

    @Override
       public void onBackPressed(){


           super.onBackPressed();      
       }        


    private class LoadViewTask extends AsyncTask<Void, Integer, Void>  
    {  
        //Before running code in separate thread  
        @Override  
        protected void onPreExecute()  
        {  
             progressDialog = ProgressDialog.show(Login.this,"Loading...",  
                    "Loading application View, please wait...", false, false);  
          progressDialog.show(); 
        }  


        @Override  
        protected Void doInBackground(Void... params)  
        {  

            try  
            {  

                synchronized (this)  
                {  

                    int counter = 0;  

                    while(counter <= 4)  
                    {  

                        this.wait(850);  

                        counter++;  

                        publishProgress(counter*25);  
                    }  
                }  
            }  
            catch (InterruptedException e)  
            {  
                e.printStackTrace();  
            }  
            return null;  
        }  


        @Override  
        protected void onProgressUpdate(Integer... values)  
        {  

            progressDialog.setProgress(values[0]);  
        }  

        @Override  
        protected void onPostExecute(Void result)  
        {  

            progressDialog.dismiss();  


        }  

    }  

            private boolean CheckPassword(String password) {

                return PASSWORD_PATTERN.matcher(password).matches();
            }

            private boolean CheckUsername(String username) {

                return USERNAME_PATTERN.matcher(username).matches();
            }
            @SuppressWarnings("deprecation")
            public void showAlertDialog(Context context, String title, String message, Boolean status) {
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();


                alertDialog.setTitle(title);


                alertDialog.setMessage(message);


                alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);


                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });


                alertDialog.show();
            }

    }

Aucun commentaire:

Enregistrer un commentaire