I have an API (here) that outputs a JSON string as output. I want to parse this JSON. It was working a while ago, but I might have changed something in my API or java code.
This is the code I'm using:
Here, msg
is a string which contains the api response
try {
/* this prints */
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
JSONObject jObject = new JSONObject(msg);
String cleaner_id = jObject.getString("cleaner_id");
/* but this does not */
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
catch(Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
I'm using codeigniter framework in php.
This is the function in my php model :
public function get_cleaner($mobile, $pass){
$this->db->select("cleaner.cleaner_id, cleaner.cleaner_name, cleaner.date_of_joining, cleaner.current_address, cleaner.mobile1, cleaner.mobile2, cleaner.date_of_birth, skill.skill_name as cleaner_designation");
$this->db->from("cleaner");
$this->db->join('skill', 'cleaner.designation = skill.skill_id', 'left');
$this->db->where("mobile1", $mobile);
$this->db->where("user_pass", $pass);
$data = $this->db->get()->result_array();
return $data;
}
And this is my php controller :
public function getCleaner(){
$mobile_no = $this->input->get('mobile');
$pass = $this->input->get('pass');
if(!is_null($mobile_no) && !is_null($pass))
{
header('Content-Type: application/javascript');
$data = $this->cleaner_model->get_cleaner($mobile_no, $pass);
if(!empty($data))
echo json_encode($data[0]);
else
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
}
}
I have searched a lot and tried a lot of different solutions I found n web, but none of the solutions I have tried has worked out for me.
UPDATE :
this gives the error
this gives the error
But
this parses fine and gives me just key not found exception
Github sample json api and my api give the cannot convert string to json obj
exception, but google maps api doesn't. Is there anything wrong with my api format ??
FIXED : I finally figured out that my api output had a leading hidden character which caused the parsing error
This is a screenshot of the error I receive (Too big a screenshot, but you get the idea...)