我的 android 代码遇到问题。我正在尝试在 Android 中绘制图表。我想使用 PHP 脚本连接到 MySQL 库。我正在尝试向脚本发送一些参数,但它一直返回 null。PHP代码:
<?
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();
?>
和安卓代码:
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());
}