[英]ANDROID&PHP - Why am I getting this JSONArray parsing error
I've tried various methods to display data from MySQL into android with PHP and JSON from my hosting here , but always getting error like this我试图从我的托管从MySQL各种方法来显示数据为Android的PHP和JSON这里,却总是得到错误像这样
but when i request another link with the same JSON result, its work fine但是当我请求另一个具有相同 JSON 结果的链接时,它工作正常
i've tried to find out, and i suggested to enable JavaScript
in my hosting, but i didn't find out the reference how to enable JavaScript
in my hosting,我试图找出答案,并建议在我的主机中启用
JavaScript
,但我没有找到如何在我的主机中启用JavaScript
的参考,
here is the PHP
code to encode_JSON
这是用于
encode_JSON
的PHP
代码
<?php
include 'dbconfig.php';
$con = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select id,ask from pertanyaan";
$result = mysqli_query($con, $query);
$rows = array();
while ($r = mysqli_fetch_array($result)) {
$rows[] = $r;
}
echo json_encode(array_values($rows));
mysqli_close($con);
?>
did i using wrong code to encode_json from PHP?我是否使用了错误的代码来从 PHP 编码_json?
EDITED AFTER ADD header('Content-Type: application/json');
LogCat
notif LogCat
通知
10-24 14:16:09.010 17727-17749/flix.yudi.okhttp1 E/MainActivity: Response from url: <html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("455e95bd78dbe99a933749187199f824");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://www.zxccvvv.cuccfree.com/send_data.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
10-24 14:16:09.010 17727-17749/flix.yudi.okhttp1 E/MainActivity: Json parsing error: Value <html><body><script of type java.lang.String cannot be converted to JSONArray
10-24 14:16:09.161 17727-17746/flix.yudi.okhttp1 V/RenderScript: 0x55abdd14e0 Launching thread(s), CPUs 8
Compared given two request.比较给出两个请求。 You haven't set the header in PHP script as JSON.
您尚未将 PHP 脚本中的标头设置为 JSON。 For setting Content-type header as JSON, put following code in PHP script
要将 Content-type 标头设置为 JSON,请将以下代码放入 PHP 脚本中
header('Content-Type: application/json');
It will work.它会起作用。 There is not error in your android code, as the 2nd url gives the desired result.
您的 android 代码中没有错误,因为第二个 url 给出了所需的结果。 Also I don't think there is any problem while encoding JSON.
此外,我认为编码 JSON 时没有任何问题。
While comparing two links check the Content-type
header in request, both are different.在比较两个链接时检查请求中的
Content-type
标头,两者是不同的。
You can try this.你可以试试这个。
<?php
include 'dbconfig.php';
$con = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select id,ask from pertanyaan";
$result = mysqli_query($con, $query);
while ($r = mysqli_fetch_array($result)) {
extract($r);
$rows[] = array(
"id"=>$id,
"ask"=>$ask
);
}
header('Content-Type: application/json');
echo json_encode($rows);
mysqli_close($con);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.