0

我是 android 开发的新手,我在这里有一些我为在我的移动应用程序中具有登录功能而构建的代码。我正在尝试将输入文本中输入数据的值比较到 mysql 数据库中,但似乎我没有明白这一点,我真的不知道我的代码是否在引导我进入某事,或者这只是胡说八道,可以你们在这里帮我吗?顺便说一句,我没有收到任何错误,只是单击按钮后没有结果。

这是我的代码:

**

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Edit Text
        inputEmail = (EditText) findViewById(R.id.inputEmail);
        inputPassword = (EditText) findViewById(R.id.inputPassword);

        // Create button
        Button btnSubmit = (Button) findViewById(R.id.btnLogin);

        // button click event
        btnSubmit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                new CheckLogin().execute();
            }
        });     
    }

    class CheckLogin extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Main.this);
            pDialog.setMessage("Logging in..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String eadd = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2/TheCalling/log_in.php");


            try {
                nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("eadd", eadd));
                nameValuePairs.add(new BasicNameValuePair("password", password));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppost);

                if(response.getStatusLine().getStatusCode()==200){
                    entity = response.getEntity();

                    if(entity != null){

                        InputStream instream = entity.getContent();
                        JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));

                        String inputEmail = jsonResponse.getString("eadd");
                        String inputPassword = jsonResponse.getString("password");

                        if(eadd.equals(inputEmail) && password.equals(inputPassword)){

                            SharedPreferences sp = getSharedPreferences("logindetails", 0);
                            SharedPreferences.Editor spedit = sp.edit();

                            spedit.putString("eadd", eadd);
                            spedit.putString("password", password);

                            spedit.commit();
                            Toast.makeText(getBaseContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();


                        }else{
                            Toast.makeText(getBaseContext(), "Invalid login details", Toast.LENGTH_SHORT).show();                   
                        }

                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

**

这是我的 php 文件:

**

<?php

$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "sample_login";
$response = array();

    $db_connect = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
    $db = mysql_select_db($db_name);
    $eadd = $_POST['eadd'];
    $password = $_POST['password'];

    // mysql inserting a new row
    $result = mysql_query("SELECT * FROM users WHERE eadd = '$eadd' and password = '$password'");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Login succes";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);   
}
?>

**

4

2 回答 2

0

1)你可以发布方法'convertStreamToString'吗?

2) 你检查过

  echo json_encode($response); 

返回正确的值并且不返回null?

于 2013-01-27T04:34:14.243 回答
0

我不是 Android 开发人员,但我要说的是。

  1. php文件一切正常。唯一的问题是,您没有返回您似乎在 android 文件中访问的电子邮件和密码。这里:

    InputStream instream = entity.getContent(); JSONObject jsonResponse = new JSONObject(convertStreamToString(instream)); 字符串 inputEmail = jsonResponse.getString("eadd"); 字符串输入密码 = jsonResponse.getString("密码");

  2. 如果查询正确,则 mysql_query() 成功返回资源,错误则返回 FALSE。您需要检查 mysql_num_rows($result) > 0 以获得有效匹配。参考http://php.net/manual/en/function.mysql-num-rows.php

记住

if ($result) {
}

除非您的查询错误,否则将始终为真。

于 2013-01-26T13:50:12.277 回答