0

これは、データベースからデータを取得するために使用しているphpコードです

<?php
mysql_connect("localhost","root","");
mysql_select_db("employee tracking");
$p = "pass";
$query="select password from user_login where user_login.E_ID=" . $_POST['EID'];
$sql=mysql_query($query);
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

これはアンドロイドコードです

private class myClass extends AsyncTask<String, Void, Long> {
        String url = "http://192.168.164.1/dbLogin.php";
        @Override
        protected Long doInBackground(String... params) {
            // TODO Auto-generated method stub

            Log.d("Debug", "Valid");
            int sc;
            StringBuilder sb = new StringBuilder();
             HttpParams httpParams = new BasicHttpParams();
             HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
             HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            DefaultHttpClient client = new DefaultHttpClient(httpParams);
            HttpPost httpGet = new HttpPost(url);
            try {
                ArrayList<NameValuePair> vp = new ArrayList<NameValuePair>();
                Log.d("Parameter", params[1]);
                vp.add( new BasicNameValuePair("EID", params[0]) );
                httpGet.setEntity((new UrlEncodedFormEntity(vp)));
                BasicHttpResponse res = (BasicHttpResponse) client.execute(httpGet);
                Log.v("response code", res.getStatusLine().getStatusCode() + ""); 
                StatusLine sl = res.getStatusLine();
                sc = sl.getStatusCode();
                Log.d("Status", Integer.toString(sc));
                if( sc == 200 ) {

                    HttpEntity entity = res.getEntity();
                    InputStream is = entity.getContent();
                    BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
                    String line ;
                    while( ( line = reader.readLine()) != null  ) {
                        sb.append(line);
                    }

                    Log.d("Out", sb.toString() );   
                    String checkString = sb.toString();
                    int cur = 0;
                    while( checkString.charAt(cur) != ':' ) cur ++;
                    cur += 2 ;
                    String pass = "";
                    while( checkString.charAt(cur) != '"') {
                        pass += checkString.charAt(cur);
                        cur ++;
                    }

                    if( pass.equals(params[1])) {
                        Intent ourIntent = new Intent("com.employeetracking.project.MENU");
                        startActivity(ourIntent);
                    }

                }

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

            return null;
        }

    }

私が直面している問題は、上記のコードをエミュレータで実行すると正しい結果が得られることです。しかし、Android でアプリケーションを実行すると、データは取得されません。Android を使用して、wi-fi 経由でラップトップのデータベースに接続しています。誰でも私が問題を理解するのを手伝ってくれますか。ありがとう。

4

1 に答える 1

0

ローカルサーバーを使用しているため

IP= 10.0.2.2を使用

String url = "http://10.0.2.2/dbLogin.php";

これにより、 Android Emulatorでローカル サーバーにアクセスできるようになります。

あなたの答えが得られることを願っています。

于 2012-08-25T13:23:17.320 に答える