Reputation: 5
I'm using the code below for Android JSON parsing but I am getting the error:
org.json.JSONException: End of input at character 0 of
03-23 13:54:28.905: W/System.err(1448): org.json.JSONException: End of input at character 0 of
public void fetchOriginatorName()
{
try {
URL url = new URL("http://financemyhome.com/webservice/bio.php");
URLConnection urlconn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
System.out.println("Line = "+line);
System.out.println("Length = "+line.length());
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++)
{
JSONObject jo = (JSONObject) ja.get(i);
id.add(jo.getString("id").toString());
name.add(jo.getString("first_name").toString()+jo.getString("last_name"));
designation.add(jo.getString("designation").toString());
email.add(jo.getString("email").toString());
cell.add(jo.getString("phone").toString());
nmls.add(jo.getString("nmls").toString());
imageurl.add(jo.getString("picture-path").toString());
office.add(jo.getString("address"));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Upvotes: 0
Views: 1063
Reputation: 68187
You are parsing your Json in wrong way. Instead of reading line by line, you should save the entire json in a string variable and then start reading from JSONArray.
for example:
JSONArray ja = new JSONArray("your_entire json_string");
for (int i = 0; i < ja.length(); i++)
{
JSONObject jo = (JSONObject) ja.get(i);
id.add(jo.getString("id").toString());
name.add(jo.getString("first_name").toString()+jo.getString("last_name"));
designation.add(jo.getString("designation").toString());
email.add(jo.getString("email").toString());
cell.add(jo.getString("phone").toString());
nmls.add(jo.getString("nmls").toString());
imageurl.add(jo.getString("picture-path").toString());
office.add(jo.getString("address"));
}
Upvotes: 5