0

我的 php 函数有问题,但我不知道在哪里!有人可以帮助我吗?java 类记录我Log: entry corrupt or truncated,当我解析 json 数据时返回JSONException: End of input at character 0 of。从调试中我看到它返回一个空字符串“”。

这是php代码:

...
if ($tag == 'getFollowing') {
   // Request type is getFollowing
    $user_id = intval($_POST['user_id']);
     $following = $db->getFollowing($user_id);  
 if ($following) {
            //following get successfully
            $response['success'] = 1;
    $response['following'] = $following;
    echo json_encode($response);
     } else {
            // following failed
            $response['error'] = 1;
            $response['error_msg'] = 'Error occured in getting following';
            echo json_encode($response);
     }
} else {
    echo "Invalid Request";
}
...
?>
...
/**
 * returns followings
 */
public function getFollowing($follower_id) {

    $result = mysql_query("SELECT followed_id FROM follows WHERE follower_id = '$follower_id'");

    $no_of_rows = mysql_num_rows($result);
$return_arr = array($no_of_rows);
    if ($no_of_rows > 0) {

    while ($row = mysql_fetch_array($result)) {
        $followed_id = $row['followed_id'];
        $followed = getUserById($followed_id);
        array_push($return_arr, $followed);
    }
}
    return $return_arr;
}

和java代码:

public List<JSONObject> getUserFollowing(String user_id) throws JSONException {
    jsonParser = new JSONParser();
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", getFollowing_tag));
    params.add(new BasicNameValuePair("user_id", user_id));
    // getting JSON Object
    JSONObject json = jsonParser.getJSONFromUrl(followURL, params);

    JSONArray arrayUsers = (JSONArray) json.get("following");
    List<JSONObject> users = new ArrayList<JSONObject>(); 

    for (int i=0; i<arrayUsers.length(); i++){
        JSONObject jObj = arrayUsers.getJSONObject(i);
        users.add(jObj);
    }
    // return json
    return users;
}

jsonParser 和 getUserById 与其他函数完美配合。那么我哪里错了?

4

1 回答 1

0

我是这样解决的

 public function getFollowing($follower_id) {
    $return_arr = array();
    $result = mysql_query("SELECT * FROM follows WHERE follower_id = '$follower_id'");

    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {

        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $followed_id = $row['followed_id'];
            $followed = $this->getUserById($followed_id);
            array_push($return_arr, $followed);
        }
        return $return_arr;
    } else {
        // user not found
        return false;
    }
}

问题是 $this 调用是必要的

$followed = $this->getUserById($followed_id);
于 2013-09-06T11:45:27.273 回答