从服务器解析json数据时出错

时间:2016-08-04 21:02:47

标签: php android json

我想在数据库中搜索一些内容,然后通过POST方法将要搜索的单词发送到服务器,我检查POST是否正常工作但是当我想获取与搜索到的工作相关的数据时,它会生成这个错误:

 error in AdsParser inparse () ->org.json.JSONException: End of input at character 1 of

我之前检查过并且我的解析器类没有任何问题,因为在我的下面的PHP代码中没有搜索查询它完全获得了json数据, 这是我的PHP代码:

<?php

require_once("include.php");

$tbl_name = "ads";
$order_by = "id";

if(isset($_POST['search_key']))
{

    $key = $_POST['search_key'];

    $connect = @mysqli_connect( $hostname , $username , $password , $database );
    if( $connect )
    {
        @mysqli_query( $connect , "SET CHARACTER SET utf8;" );

        $my_query = "SELECT * FROM ".$tbl_name.
                        " WHERE title LIKE '%".$key.
                        "%' OR intro LIKE '%".$key.
                        "%' ORDER BY ".$order_by;

        $result = @mysqli_query( $connect , $my_query );
        if( $result )
        {
            $response['ads'] = array();
            $response['success'] = 1;

            while( $row = @mysqli_fetch_array( $result ) )
            {
                $ads = array();


                $ads['id']  = $row['id'];
                $ads['title']   = $row['title'];
                $ads['cost']    = $row['cost']; 
                $ads['costunit']= $row['costunit']; 
                $ads['intro']   = $row['intro'];
                        $ads['image']   = $sitename . $row['image'];
                $ads['seller']  = $row['seller'];
                $ads['phone']   = $row['phone'];        
                $ads['date']    = ago($row['date']);


                $q = "SELECT name FROM cat WHERE id='".$row['cat_id']."'";
                $temp1 = @mysqli_query($connect , $q);
                $temp2 = @mysqli_fetch_row($temp1);
                $cat = $temp2[0];

                $q1 = "SELECT name FROM cities WHERE id='".$row['city_id']."'";
                $temp3 = @mysqli_query($connect , $q1);
                $temp4 = @mysqli_fetch_row($temp3);
                $city  = $temp4[0];

                $ads['city']    = $city;    
                $ads['cat'] = $cat;         

                array_push( $response['ads'] , $ads);
            }
        }
        else
        {
            $response['success'] = 0;
            $response['message'] = "nothing";
        }

        echo( json_encode( $response ) );

        /* I don't want use JSON_PRETTY_PRINT. If you want, you know! */

        @mysqli_close( $connect );
    }
}

function ago( $datetime )
{

$interval = date_create('now')->diff( date_create($datetime) );
if ( $v = $interval->y >= 1 ) {
    $in = ($interval->y == 1) ? ' سال قبل' : ' سال قبل';
    return $interval->y . $in;
}
if ( $v = $interval->m >= 1 ) {
    $in = ($interval->m == 1) ? ' ماه قبل' : ' ماه قبل';
    return $interval->m . $in;
}
if ( $v = $interval->d >= 1 ) {
    $in = ($interval->d == 1) ? ' روز قبل' : ' روز قبل';
    return $interval->d . $in;
}
if ( $v = $interval->h >= 1 ) {
    $in = ($interval->h == 1) ? ' ساعت قبل' : ' ساعت قبل';
    return $interval->h . $in;
}
if ( $v = $interval->i >= 1 ) {
    $in = ($interval->i == 1) ? ' دقیقه قبل' : ' دقیقه قبل';
    return $interval->i . $in;
}
$in = ($interval->s == 1) ? ' ثانیه قبل' : ' ثانیه قبل';
return $interval->s . $in;
}   
?>

0 个答案:

没有答案