【问题标题】:json parse error on line 1: < html > < body >第1行的json解析错误:<html><body>
【发布时间】:2017-02-07 19:49:32
【问题描述】:

我不知道为什么代码不起作用,它应该编码为 json ? 当我从 Android Studio 中的应用程序调用此 PHP 代码链接时,它不起作用并显示错误消息,它显示此代码:

< 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("fafd97787619720460a70e34d00aedb4");
document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
location.href = "http://gmarket.byethost5.com/json_getS.php?i=2"; 

在 jsonlint 网站测试 php URL 并返回此错误时也显示与上述相同:

第 1 行解析错误:

这个 php 链接:http://gmarket.byethost5.com/json_getS.php

我使用的这个 php 代码:

<?php  
$host= "sql201.byethost5.com";
$user = "xxxxxxx";  
$pass = "xxxxxxx";  
$db = "xxxxxxx";  

$sql="select * from customer";

$con = mysqli_connect($host,$user,$pass,$db);
$result= mysqli_query($con,$sql);

$response = array();

while ($row = mysqli_fetch_array($result))
{

 array_push($response,array("name"=>$row[0],"phone"=>$row[1] ));
}
echo json_encode(array("customers"=>$response));

mysqli_close($con);
?> 

【问题讨论】:

  • URL 返回 json。看起来问题出在 Android 代码中
  • $response = array(); while ($row = mysqli_fetch_array($result)) { $response['customers'][] = array("name"=&gt;$row[0],"phone"=&gt;$row[1]); } echo json_encode($response);

标签: java php android json jsonlint


【解决方案1】:

你的 Json 是正确的,问题是你返回的 HTML,你没有告诉浏览器内容是 Json。

使用

header('Content-Type: application/json');
echo json_encode($response);

【讨论】:

  • 我在应用中使用过,没有任何变化,同样的问题>
【解决方案2】:

我使用了另一个虚拟主机域 (000webhost) 和它的工作没有任何问题与相同的代码,我认为问题来自 (byethost) 而不是来自代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2013-06-03
    相关资源
    最近更新 更多