3

データベースと Android アプリの接続に問題があります。このチュートリアルを実装しようとしています。すべてがうまくいっているようですが、エラーではなく成功も得られません。

クリックすると 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投稿方法は次のとおりです。

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 応答) の中間値をログに記録しようとしたところ、ポスト メソッドが実行されましたが、何もログに記録されていません。すべてのデータベース環境で、「localhost」を「127.0.0.1」に変更し、公開されている Web ホストにも変更しようとしましたが、成功しませんでした。これらはすべてエミュレーターとパブリックホストで、実際のデバイスでも試しました。ブラウザから確認するとサーバーが起動しているようです。値を持つデータベースが存在します。実行中のすべてのサービス (apache、mysql)。

主な問題は、エラーがないことです。何がうまくいかないのですか?同じ問題を抱えている人を見つけることができませんでした。

4

1 に答える 1

1

問題は-->PHPコードにありました。==またはに変更すると>、すべてが正常に機能します。

于 2012-05-27T08:41:29.723 に答える