1

我按照他的流行教程将 Android 连接到 MySQL: http ://www.helloandroid.com/tutorials/connecting-mysql-database

它非常擅长将数据编码回 JSON 并显示数据行。

我的问题:我只想返回一个值,一个从 PHP 到 Java 的变量,什么 id?

这是我的PHP:

$sql="SELECT AVG(rating) FROM ratings WHERE item_id = 1";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
echo $row[0];

我想根据上述教程的指导将最后一个“回声”发送回 Android,我该怎么做?

4

2 回答 2

0

如果您使用的是 WebView,请尝试addJavascriptInterface(new JavaScriptInterface(this), "Android") http://developer.android.com/guide/webapps/webview.html并调用

<script type="text/javascript">
  function showAndroidToast(avg_value) {
    Android.echo(avg_value);
  }
</script>
于 2012-06-21T23:26:32.453 回答
0

$row 是一个数组,它的值应该是:

$row['AVG(rating)'] = <some value>

您需要做的就是将json_encode函数扔到行周围并回显

print(json_encode($row));

This should get you the single value json encoded string that the java code can read.

于 2012-06-21T23:32:14.747 回答