我正在尝试制作一个在应用程序中使用用户名和密码的应用程序,并使用http请求将其发送到mysql数据库,在检查数据库中是否存在用户后,我在textview中看到显示的文本,该应用程序中显示“正确的用户名和密码”,如果不存在,我会显示一个文本“错误的用户名或密码”
单击“登录”后,我看不到“正确”或“不正确”的任何内容

Logindb类

    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文件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>`

最佳答案

我对此工作完全感到惊讶,并且不会崩溃,您正在尝试从doInBackground(通过在postData中调用res.setText)访问UI,这是不可能的。您应该在doInBackground中返回一个布尔值,并根据该值在onPostExecute中相应地更改TextViews。

使用它作为类定义:

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


然后在公共布尔postData(String a,String b)的末尾:

return result.equals("0")


在doInBackground中:

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


最后

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();
}

关于php - Android AsyncTask中的Http请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20146417/

10-12 04:48