JSON-Validator:响应测试vaild但URL测试不是

时间:2016-10-05 20:34:48

标签: php android json android-volley

我做了一个简单的json响应代码但是在ANDROID中它被作为一个字符串而不是一个对象,所以我去了一个JSON Validator WebSite。 当我复制响应时,它说JSON是Vaild,但是当我通过URL检查它时,它显示了以下内容:

Error:Invalid encoding, expecting UTF-8, UTF-16 or UTF-32.[Code 29, Structure 0]
Error:Expecting object or array, not string.[Code 1, Structure 1]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 1]

Json回复:

{"questions":[{"id":"1","user_id":"1","city":"1","question":"we","likes":"0","img":"0","req_date":"2016-10-03 23:06:06","qort":"q","user_name":"basel","user_img":"0","comments":"$num"},{"id":"2","user_id":"1","city":"1","question":"\u0633\u064a \u0634\u0633\u064a\u0628\u0633 \u0628\u064a\u0633 \u0628\u064a\u0633","likes":"0","img":"0","req_date":"2016-10-03 23:30:44","qort":"q","user_name":"basel","user_img":"0","comments":"0"},{"id":"3","user_id":"3","city":"2","question":"\u0633\u064a \u0634\u0633\u064a2222\u0628\u0633 \u0628\u064a\u0633 \u0628\u064a\u0633","likes":"0","img":"0","req_date":"2016-10-03 23:32:06","qort":"q","user_name":"\u0645\u062d\u0645\u062f \u0627\u0644\u0633\u0639\u064a\u062f\u064a","user_img":"","comments":"0"},{"id":"4","user_id":"1","city":"1","question":"\u0645\u0631\u062d\u0628\u0627 \u0645\u0631\u062d\u0628\u0627 \u062a\u0633\u064a\u0633\u064a \u062a\u0633\u064a \u0633\u062a\u0646\u064a\u0645 \u0633\u064a \u0633\u0646\u064a \u0633\u0646\u0645\u064a \u0633\u0646 \u0646\u0633\u0634\u064a \u0649\u0646\u0645\u0629","likes":"0","img":"0","req_date":"2016-10-03 23:32:59","qort":"q","user_name":"basel","user_img":"0","comments":"0"},{"id":"5","user_id":"1","city":"1","question":"\u0645\u0631\u062d\u0628\u0627 \u0645\u0631\u062d\u0628\u0627 \u062a\u0633\u064a\u0633\u064a \u062a\u0633\u064a \u0633\u062a\u0646\u064a\u0645 \u0633\u064a \u0633\u0646\u064a \u0633\u0646\u0645\u064a \u0633\u0646 \u0646\u0633\u0634\u064a \u0649\u0646\u0645\u0629","likes":"0","img":"0","req_date":"2016-10-04 00:18:41","qort":"q","user_name":"basel","user_img":"0","comments":"0"}]}

漂亮格式的JSON:

{
    "questions": [
        {
            "id": "1",
            "user_id": "1",
            "city": "1",
            "question": "we",
            "likes": "0",
            "img": "0",
            "req_date": "2016-10-03 23:06:06",
            "qort": "q",
            "user_name": "basel",
            "user_img": "0",
            "comments": "$num"
        },
        {
            "id": "2",
            "user_id": "1",
            "city": "1",
            "question": "سي شسيبس بيس بيس",
            "likes": "0",
            "img": "0",
            "req_date": "2016-10-03 23:30:44",
            "qort": "q",
            "user_name": "basel",
            "user_img": "0",
            "comments": "0"
        },
        {
            "id": "3",
            "user_id": "3",
            "city": "2",
            "question": "سي شسي2222بس بيس بيس",
            "likes": "0",
            "img": "0",
            "req_date": "2016-10-03 23:32:06",
            "qort": "q",
            "user_name": "محمد السعيدي",
            "user_img": "",
            "comments": "0"
        },
        {
            "id": "4",
            "user_id": "1",
            "city": "1",
            "question": "مرحبا مرحبا تسيسي تسي ستنيم سي سني سنمي سن نسشي ىنمة",
            "likes": "0",
            "img": "0",
            "req_date": "2016-10-03 23:32:59",
            "qort": "q",
            "user_name": "basel",
            "user_img": "0",
            "comments": "0"
        },
        {
            "id": "5",
            "user_id": "1",
            "city": "1",
            "question": "مرحبا مرحبا تسيسي تسي ستنيم سي سني سنمي سن نسشي ىنمة",
            "likes": "0",
            "img": "0",
            "req_date": "2016-10-04 00:18:41",
            "qort": "q",
            "user_name": "basel",
            "user_img": "0",
            "comments": "0"
        }
    ]
}

PHP代码:

<?php

header("Content-Type:application/json; charset=utf-8");
include_once("config.php");

class Questions
{
    public $id;
    public $user_id;
    public $city;
    public $question;
    public $likes;
    public $img;
    public $req_date;
    public $qort;
    //public $user_name;
    //public $user_img;
    //public $comments;
    //didnt help
}                               

function getQ (){
    $sql= "select * from `questions`";
    $result = mysql_query($sql);    
    if (mysql_num_rows($result) > 0){   
    $ques=array();
    while ($ques_info= mysql_fetch_array($result)){ 
        $uid = $ques_info['user_id'];
        $qid = $ques_info['id'];
        $info = new Questions();
        $info->qort= 'q';   
        $info->id= $ques_info['id'];        
        $info->user_id=$ques_info['user_id'];       
        $info->city=$ques_info['city'];             
        $info->question=$ques_info['question'];                     
        $info->img='0';         
        $info->likes=$ques_info['likes'];               
        $info->req_date=$ques_info['req_date'];             
            $sqluname = "select * from `users` where `id`='".$uid."'";
            $resultuname = mysql_query($sqluname);
            if (mysql_num_rows($resultuname) > 0){
                while ($user_info= mysql_fetch_array($resultuname)){
                $info->user_name= $user_info['name'];
                $info->user_img= $user_info['img'];
                }
            }
            $sqlcomments = "select * from `question_comments` where `question_id`='".$qid."'";
            $resultcomments = mysql_query($sqlcomments );
            $num = mysql_num_rows($resultcomments);
            if ($num > 0){
                $info->comments= '$num';
            }else{
            $info->comments= '0';
            }
        array_push($ques, $info);
    }
        echo json_encode(array('questions' => $ques), true);
    }
}

switch ($_GET['do'])
{   
    case 'getQ':
    {
        getQ ();
    }
}

我要发疯了什么不对吗? 在android我正在使用Voelly im获取错误类型java.lang.string无法转换为JSONObject

android代码:

   private void fetchPosts() {
        Log.e("ir", "fetchPosts");
        JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                    try {
                        Log.e("ir", "onResponse");
                        Log.e("ir", "GONE");
                        JSONArray response = jsonObject.getJSONArray("questions");
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject obj = response.getJSONObject(i);
                            qlist ads = new qlist();
                            ads.setQort(obj.getString("qort"));
                            ads.setIdquestion(obj.getString("id"));
                            ads.setIduser(obj.getString("user_id"));
                            ads.setQuestion(obj.getString("question"));
                            ads.setLikes(obj.getString("likes"));
                            ads.setComments(obj.getString("comments"));
                            ads.setImg(obj.getString("img"));
                            ads.setReq_date(obj.getString("req_date"));
                            ads.setUser_name(obj.getString("user_name"));
                            ads.setUser_img(obj.getString("user_img"));
                            postsList.add(ads);
                            Log.e("ir", "questions");
                        }
                        getManshateAd();
                    } catch (JSONException e) {
                        Log.e("ir", "JSONException");
                        e.printStackTrace();
                    }
                    adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Log.e("ir", "onErrorResponse"+arg0);

            }
        }) {
        @Override
        public Request.Priority getPriority() {
            return Request.Priority.IMMEDIATE;
        }
        };
        postReq.setRetryPolicy(new DefaultRetryPolicy(50000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(postReq);
    }

提前感谢。

1 个答案:

答案 0 :(得分:1)

检查你的respose编码。也许您的回复包含BOM符号。