在我的 Android 应用程序中,我尝试与保存随机生成的假用户名和分数的 Web 服务器进行通信。
{ scores: [
{
un: "Feltricapulta",
sc: "143"
},
{
un: "Henroid",
sc: "120"
},
{
un: "ieteubmospta",
sc: "70"
},
{
un: "pmbotesteuai",
sc: "67"
},
{
un: "epesomiubtat",
sc: "65"
}
] }
PHP 文件中的代码如下所示:
<?php
include ('connecttomysql.php');
$command = 'SELECT un, sc FROM xmlscores ORDER BY sc DESC';
$execute_command = mysql_query($command);
echo '{ "scores": ';
while ($table_row = mysql_fetch_assoc($execute_command))
{
$jsonArray [] = $table_row;
}
echo json_encode($jsonArray);
echo '}'
?>
我已经这样称呼过.php
Android 中使用通用 URLHttpGet
方法。 json 数据的输出打印到堆栈跟踪,并向我提供“无法从对象转换为数组”错误。
查看 PHP 文件和 json 输出,我的代码或输出有什么明显的错误吗?我想不通。
“无法从对象转换为数组”
意味着您正在尝试将响应字符串转换为JSONArray
。但当前字符串包含JSONObject
作为根元素而不是JSONArray
。所以将其转换为JSONObject
as:
JSONObject json=new JSONObject(<Server response string here>);
// get scores JSONArray from json
JSONArray jsonscoresarray=json.getJSONArray("scores");
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)