2012-03-23 124 views
0

我使用下面的Android JSON解析的代码,但我得到的错误:Android的JSON解析错误

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

把你的全logcat的和你的JSON性反应的有效.... – 2012-03-23 14:03:22

回答

5

您分析您的JSON以错误的方式。您应该将整个json保存在字符串变量中,而不是逐行读取,然后从JSONArray开始读取。

例如:

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

您也可以使用*流*解析器像[杰克逊](HTTP://jackson.codehaus。组织/)。 – dmon 2012-03-23 14:09:57

+0

,或者你也可以使用像'Gson'这样的流解析器:) – waqaslam 2012-03-23 14:10:39

+0

真正优化它,你应该在循环之前声明jo并在每次迭代中重用它。 – 2012-03-23 14:56:05