douyi0219 2016-10-24 06:46
浏览 171
已采纳

ANDROID和PHP - 为什么我得到这个JSONArray解析错误

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 Screenshot_2016-10-22-15-52-56-359_flix.yudi.okh.png

but when i request another link with the same JSON result, its work fine

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,

here is the PHP code to encode_JSON

<?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?

EDITED AFTER ADD header('Content-Type: application/json');

LogCat notif

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
  • 写回答

2条回答 默认 最新

  • dshm8998473 2016-10-24 07:02
    关注

    Compared given two request. You haven't set the header in PHP script as JSON. For setting Content-type header as JSON, put following code in PHP script

    header('Content-Type: application/json');
    

    It will work. There is not error in your android code, as the 2nd url gives the desired result. Also I don't think there is any problem while encoding JSON.

    While comparing two links check the Content-type header in request, both are different. enter image description here

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误