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 回答