Search code examples
javaandroidandroid-asynctask

"getText" syntax is not working and it's showing error


"getText" syntax is not working and it's showing error.

    @Override
    protected String doInBackground(String... params)
    {
        String usernam = username.getText().toString();
        String passwordd = password.getText().toString();
        if(usernam.trim().equals("")|| passwordd.trim().equals(""))
        {
            z = "Please enter Username and Password";
        }
        else
        {
            try
            {
                con = connectionclass(un, pass, db, ip);
                if (con == null)
                {
                    z = "Check Tour Internet Access!";
                }
                else
                {
                    String query = "select * from login where user_name= '" + usernam.toString() + "' and pass_word = ' " + passwordd.toString();
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    if(rs.next())
                    {
                        z = "Login successful";
                        isSuccess = true;
                        con.close();
                    }
                    else
                    {
                        z = "Invalid Credentials";
                        isSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                z = ex.getMessage();
            }
        }
        return z;
    }

i am not able to get answer, expected answer is, should able retrieve username, password and match it to the username password which we enter, but now i couldn't able to match both.


Solution

  • private class MyAsyncTask extends AsyncTask<String, Void, String> {
    
     private String username="";
     private String password="";
    
     public MyAsyncTask(String user, String pass)
     {
        this.username = user;
        this.password = pass;
     }
    
     protected void onPreExecute() {
    
     }
    
     protected Bitmap doInBackground(String... strings) {
         // Some long-running task like downloading an image.
         // No UI related work here.. or else it will crash
    
     }
    
    
    
     protected void onPostExecute(Bitmap result) {
         // This method is executed in the UIThread
         // with access to the result of the long running task
    
     }
    }
    

    Now when calling the asynctask in your Activity do this

    // Its inside the Activity Class 
    MyAsyncTask asd = new MyAsyncTask(username.getText().toString(),password.getText().toString());
    
    asd.execute("");
    

    Pass the values of the EditText while calling the AsyncTask itself in the activity