我编写了一个 Android 应用程序来从本地数据库中检索一些数据。我创建了我的 php 文件来进行查询和一个类来将查询发送到 db。
这是php文件:
<?php
$db_host = "10.0.2.2";
$db_name = " name";
$db_user = " usr";
$db_password = " pwd";
//connessione al database
$db = mysql_connect($db_host, $db_user, $db_password);
if ($db == FALSE) die ("Errore nella connessione. Verificare i parametri nel file onnection.php");
mysql_select_db($db_name, $db)
or die ("Errore nella selezione del database. Verificare i parametri nel file onnection.php");
//preleviamo la query passataci dall’applicazione
$query = $_REQUEST['querySend'];
//eseguiamo la query
$result = mysql_query ($query);
while($e=mysql_fetch_assoc($result))
$output[]=$e;
//stampiamo il risultato in formato json
print(json_encode($output));
?>
和我的班级发送查询:
String result = "0";
InputStream is = null;
//the query to send
ArrayList<NameValuePair> querySend = new ArrayList<NameValuePair>();
querySend.add(new BasicNameValuePair("querySend",query));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/query.php");
httppost.setEntity(new UrlEncodedFormEntity(querySend));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(
new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result: "+e.toString());
}
return result;
我收到 400 错误。有什么建议么?