I return JSON for my android application, I have done the same procedure in terms of returning it to the app multiple times within the project, but this call doesn't show anything from the buffered reader
because when I call the URL directly, there is a space at the top. (See image)
This is the php code which json_encodes
the response.
require '../repository/resultRepository.php';
header('content-type: application/json');
$results = resultRepository::getResultSet();
if (empty($results)){
$results["error"] = TRUE;
$results["error_msg"] = "Problem when bringing back results";
echo json_encode($results);
}else{
echo json_encode($results);
}
This is the code which calls the db.
public static function getResultSet(){
$results = array("error" => FALSE);
$db_results = dbClass::query("select rs.userid as id, q.question as question, an.answer as rightanswer, a.answer as useranswer, rs.correct from resultset rs
inner join questions q on q.id = rs.questionid
inner join answers a on a.id = rs.answerid
join answers an on an.id = q.answerid");
foreach ($db_results as $db_result) {
$result = new result(
$db_result->id,
$db_result->question,
$db_result->rightanswer,
$db_result->useranswer,
$db_result->correct);
array_push($results, $result);
}
return $results;
}
This is not a problem related to your android application. If you still want to use this output on your app, you can use to trim()
to get rid of unwanted spaces at start and end.