2014-03-02 31 views
0

我初學者到Android。我想提出一個有關webservice.I應用程序正在通過點擊URL獲取JSON和我想在列表視圖中顯示。我試了很多方法,但沒有result.I得到了JSON exceptions.please幫我 這是我的JSON:的Android JSON空指針異常而解析

[ 
    [ 
    { 
    "id":"9637", 
    "country":"Australia", 
    "time":"14:00", 
    "type":"country", 
    "status":"good" 
    }, 
    { 
    "id":"9638", 
    "country":"india", 
    "time":"16:00", 
    "type":"country", 
    "status":"good" 
    } 
    ] 
    ] 
code: 
class Response extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      try { 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       jsonResponse = EntityUtils.toString(httpEntity); 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 
     } 

protected void onPostExecute(Void result) { 

      Toast.makeText(MainActivity.this,jsonResponse,Toast.LENGTH_SHORT).show(); 
      try { 
       JSONArray jsonArray=new JSONArray(jsonResponse); 
       for(int i=0;i<jsonArray.length();i++) 
       { 
        Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show(); 

       } 
      // JSONArray jsonArray=jsonObject.get 


      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 



      super.onPostExecute(result); 
     } 
+2

哪裏的代碼,其中的異常堆棧跟蹤? – laalto

+0

丟失的報價@'「狀態」:「好」' – Raghunandan

+0

發表您的有關處理JSON字符串 –

回答

1

我覺得這個代碼是正確的:

 try { 
      JSONArray json=new JSONArray(jsonResponse); 
      JSONArray jsonArray=json.getJSONArray(0); 
      for(int i=0;i<jsonArray.length();i++) 
      { 
       Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show(); 

      } 
     // JSONArray jsonArray=jsonObject.get 


     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

,因爲你的JSON響應是一個數組的數組。