这是我用来从我的数据库中检索数据的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();
?>
这是android代码
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连接到我的笔记本电脑中的数据库。任何人都可以帮我解决问题。感谢。
答案 0 :(得分:0)
当您使用本地服务器时
使用 IP = 10.0.2.2
String url = "http://10.0.2.2/dbLogin.php";
这样您就可以在 Android模拟器
中访问本地服务器希望你得到答案。