我遇到了我的android代碼的麻煩。我正在嘗試在Android中繪製一個圖表。我想使用PHP腳本連接到MySQL基礎。我試圖發送一些參數給腳本,但它一直返回null。 PHP代碼:從PHP返回特定數量的值到Android
<?
mysql_connect(...);
mysql_select_db("temperature");
$Vreme = $_POST['Vreme'];
$Datum = $_POST['Datum'];
$q = mysql_query("SELECT * FROM temperature WHERE
((datum > $Datum) || (datum = $Datum)) && (vreme > $Vreme) ");
while($e = mysql_fetch_assoc($q))
$output[] = $e;
print(json_encode($output));
mysql_close();
?>
和Android代碼:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Vreme",s1));
nameValuePairs.add(new BasicNameValuePair("Datum",s2));
InputStream is = null;
try {
String adresa="http://senzori.open.telekom.rs/grafik.php";
HttpPost httppost = new HttpPost(adresa);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpclient = new DefaultHttpClient();
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());
}
嘗試打印出s1和s2值以檢查空字段,並嘗試單獨運行php腳本以查看您是否在瀏覽器中獲得值 – kabuto178
儘管我不確定awnser,但您的代碼是非常不安全的。不要使用'mysql',而是使用'mysqli'或'pdo',並且消毒你的變量。你的PHP代碼很容易被SQL注入。 –
你可以'print_r($ output)'並檢查你是否得到任何輸出?同樣如雨果提到的,您需要清理您使用過的POST變量。 – GoodSp33d