【问题标题】:Connecting mysql database with Android app将 mysql 数据库与 Android 应用程序连接
【发布时间】:2012-05-27 04:03:21
【问题描述】:

我在将数据库与 Android 应用程序连接时遇到问题。我正在尝试实现this 教程。一切似乎都很好,但我既没有成功也没有错误。

有一个按钮监听器,点击后会发布到 PHP 文件并获取结果。这是它的代码:-

    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            //String valid = "1";
            String response = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://10.0.2.2/check.php", postParameters);
                String res=response.toString();
                Log.d("res:", res);

               // res = res.trim();
                res= res.replaceAll("\\s+","");                              
                //error.setText(res);

               if(res.equals("1"))
                    error.setText("Correct Username or Password");
                else
                    error.setText("Sorry!! Incorrect Username or Password"); 
            } catch (Exception e) {
                un.setText(e.toString());
            }

        }
    });

这里是http post方法:-

public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        Log.d("postMethodReturn", result);
        return result;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

PHP代码如下:-

<?php
$un=$_POST['username'];
$pw=$_POST['password'];
//connect to the db
$user = "xyz";
$pswd = "xyz";
$db = "mydb";
$host = "localhost";
$conn = mysql_connect($host, $user, $pswd);
mysql_select_db($db);
//run the query to search for the username and password the match  
$query = "SELECT * FROM mytable WHERE user = '$un' AND pass = '$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 1;  // for correct login response  
else  
echo 0; // for incorrect login response  
?>

程序中是否有任何错误?我尝试在活动代码中记录 res(http 响应)的中间值并导致执行 post 方法,但没有记录任何内容。尝试将“localhost”更改为“127.0.0.1”,并更改为具有所有数据库环境的公开可用的虚拟主机,但没有成功。所有这些都在模拟器和公共主机上,也在真实设备上尝试过。从浏览器检查时,服务器似乎正在运行。数据库与值一起存在。正在运行的所有服务(apache、mysql)。

主要问题是没有错误!有什么建议吗? 找不到遇到同样问题的人。

【问题讨论】:

  • 你能知道打印的结果是什么吗?
  • 它总是将输出显示为 0(不正确的登录响应),不幸的是没有请求的日志。
  • 这表明prblm是php函数不在android端...所以先检查你的php连接
  • 好的,谢谢。关于如何检查的任何建议?我在 /var/www 下有 php 文件,我可以从同一目录中的浏览器中看到 index.html 文件。
  • 不错的 SQL 注入孔...享受您的 DB pwn3d。另外,在 php 语言规范中,--&gt; 运算符是在哪里定义的?

标签: php android mysql


【解决方案1】:

问题在于 PHP 代码中的 --&gt;。将其更改为 ==&gt; 一切正常!

【讨论】:

    猜你喜欢
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2019-06-16
    相关资源
    最近更新 更多