简体   繁体   中英

Android JSON parsing error

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();
        } 
    }   

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"));         
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM