user1928775
user1928775

Reputation: 355

Http Request inside an AsyncTask Android

I'm trying to make an application that takes username and password in the app and send them using a http request to mysql database, after checking in the database if the user exist , i see a text displayed in the textview saying in the application "A Correct Username and Password" and if it doesn't exist i see a text displayed "Incorrect Username or Password" i can't see neither Correct nor Incorrect i don't see anything after clicking button Login

Logindb Class

    public class Logindb extends Activity {
    Button login;
    EditText u, p;
    TextView res;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logindb);

        login = (Button) findViewById(R.id.login);
        u = (EditText) findViewById(R.id.u);
        p = (EditText) findViewById(R.id.p);
        res = (TextView) findViewById(R.id.res);
        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new MyAsyncTask().execute(u.getText().toString(), p.getText()
                        .toString());
            }
        });
    }

    private class MyAsyncTask extends AsyncTask<String, Integer, Double> {
        @Override
        protected Double doInBackground(String... params) {
            postData(params[0], params[1]);
            return null;
        }

        protected void onPostExecute(Double result) {
            Toast.makeText(getApplicationContext(), "command sent",
                    Toast.LENGTH_LONG).show();
        }

        protected void onProgressUpdate(Integer... progress) {}

        public void postData(String a, String b) {
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", a));
            postParameters.add(new BasicNameValuePair("password", b));

            String response = null;
            try {
                response = CustomHttpClient.executeHttpPost(
                        "http://192.168.1.11/new/check.php", postParameters);
                String result = response.toString();
                result = result.replaceAll("\\s+", "");
                if (!result.equals("0")) {
                    res.setText("A Correct Username and Password");
                }
                else
                    res.setText("Incorrect Username or Password");
            } catch (Exception e) {
                res.setText(e.toString());
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.logindb, menu);
        return true;
    }
}

PHP File check.php

     <?php
     $un=$_POST['username'];
     $pw=$_POST['password'];
     //connect to the db

     $host="localhost"; // Host name 
     $user="root"; // Mysql username 
     $pswd="123"; // Mysql password 
     $db="pet_home"; // Database name 
     $tbl_name="users"; // Table name

    $conn = mysql_connect($host, $user, $pswd);
    mysql_select_db($db, $conn);
    //run the query to search for the username and password the match
      $query = "SELECT * FROM $tbl_name WHERE first_name = '$un' AND password = '$pw'";

$result = mysql_query($query) or die("Unable to verify user because : ".mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)
echo mysql_result($result,0);  // for correct login response
else
 echo 0; // for incorrect login response
 ?>

logindb.xml `

     <EditText
     android:id="@+id/u"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="56dp"
    android:ems="10" />

      <EditText
    android:id="@+id/p"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/u"
    android:layout_below="@+id/u"
    android:layout_marginTop="22dp"
    android:ems="10"
    android:inputType="textPassword" />

    <Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/p"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="67dp"
    android:text="Login" />

    <TextView
    android:id="@+id/res"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/login"
    android:layout_below="@+id/p"
    android:layout_marginTop="27dp"
    android:text="OK"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/black" />

     </RelativeLayout>`

Upvotes: 0

Views: 593

Answers (3)

saiful103a
saiful103a

Reputation: 1129

When you do any kind of network operation you have to use thread/asynctask to do so as you have done. On the other hand when you have to access or modify any UI element you have to do that in MainThread(UIThread). Now what you are doing is calling res.setText() from inside of a thread which is not UIThread. AsyncTask is consist of couple of method where some method runs in UIThread like onPostExecute, onProgressUpdate .. but doInBackground does not run in UIThread and that is why you are facing this problem.

To solve your problem you can now follow other answers as they are suggesting which actually should be the way to handle this.In that case you have to adjust your code to work like this.

Also there is one other way you might solve your problem but sadly it removes the necessity of using asynctask since you are not taking any advantage of AsyncTask, you could've just used Thread instead.

When you do any modification to any UI element from inside postData(params[0], params[1]) use runOnUiThread.

Like this:

   runOnUiThread(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        // do what you have to do
                     res.setText("A Correct Username and Password");
                    }
            });

Try this way it should work.

Upvotes: 1

2Dee
2Dee

Reputation: 8627

I'm surprised this work at all and doesn't crash, you are trying to access the UI from doInBackground(by calling res.setText inside postData), which is not possible. You should return a boolean in doInBackground and according to that, change the TextViews accordingly in onPostExecute.

use this as the class definition :

private class MyAsyncTask extends AsyncTask<String, Integer, Boolean>

then at the end of the public boolean postData(String a,String b) :

return result.equals("0")

and in doInBackground :

boolean success = postData(params[0],params[1]);
return success;

and finally

protected void onPostExecute(Boolean result){
    // change the text for your TextViews here according to the Boolean result
    if (result){
        res.setText("A Correct Username and Password");
    }else{
        res.setText("Incorrect Username or Password");
    }
    Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}

Upvotes: 1

Damien R.
Damien R.

Reputation: 3373

It doesn't work because you're modifying your TextView in the doInBackground. All your visual modifications have to be called in your onPostExecute So here what you need to do is to return from postData the string result, and move the following code in your onPostExecute:

  if (!result.equals("0")) {
    //  Intent in = new Intent(Logindb.this, Welcome.class);
    // LoadPreferences();
    res.setText("A Correct Username and Password");
    //startActivity(in);
  }
  else
     res.setText("Incorrect Username or Password");

Upvotes: 2

Related Questions