dragon071111 2012-05-27 04:03
浏览 51
已采纳

使用Android应用程序连接mysql数据库

I have a problem connecting database with Android app. I am trying to implement this tutorial. Everything seems to be fine but I neither get any success not an error.

There is a button listener which on clicking does a post to a PHP file and gets the result. Here is the code for it:-

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

        }
    });

Here is the http post method:-

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

The PHP code is as below:-

<?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  
?>

Is there any bug in the program? I tried logging the intermediate values of res (http response) in activity code and result in the execute post method, but nothing is being logged. Tried changing "localhost" to "127.0.0.1" and also into a publicly available webhost, with all the database environment, but no success. All these on emulator and with public host, tried with real device too. Server seems to be running when checked from browser. Database exists with the values. All services running (apache, mysql).

The main problem is that there is no error! Any suggestions what is going wrong? Couldn't find anyone with the same problem.

  • 写回答

1条回答 默认 最新

  • dongsi8812 2012-05-27 08:41
    关注

    the problem was --> in the PHP code. changed it to == or > and everything works fine!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致