2014-11-25 24 views
1

我想使用JsonParsing獲取countdata。這怎麼可能? 我的JSON響應爲是,如何在android中獲取JsonResponse的值?

{"data":[{"l_id":"1","p_id":"5","user_id":"15"},{"l_id":"2","p_id":7","user_id":"16"}]}{"countdata":2} 

countdata是數據的總數。我能夠獲取數據但無法計數數據。請指導我。

我能爲i_id,p_id的獲取數據和USER_ID成功和我的代碼,

private class GetDetail extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      dataList = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 
      jsonobject = JSONFunctions.getJSONfromURL(urlData); 

      try { 
       // Locate the array name in JSON 
       jsonarray = jsonobject.getJSONArray("data"); 

       for (int i = 0; i < jsonarray.length(); i++) { 

        HashMap<String, String> map = new HashMap<String, String>(); 
        jsonobject = jsonarray.getJSONObject(i); 
        // Retrive JSON Objects 

        String lId=jsonobject.getString(TAG_L_ID); 
        String pId=jsonobject.getString(TAG_P_ID); 
        String userId=jsonobject.getString(TAG_User_ID); 

       } 
      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 

     } 
    } 

但我不能爲countdata獲取數據。並給出了錯誤,java.lang.IndexOutOfBoundsException:無效指數2,大小爲0

我的準則是,

private class getData extends AsyncTask<Void, Void, Void> { 
     JSONObject jsonobject; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      arr = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 
      jsonobject = JSONFunctions.getJSONfromURL(urlCount); 

      String strCount = jsonobject.getString("qdata"); 

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

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
     } 
    } 

請指導我。 在此先感謝。

+1

您的JSON是無效的。 – 2014-11-25 06:55:17

+0

@DanialHussain請給我建議,我的json有什麼錯誤?請指導我。 – Reena 2014-11-25 06:59:16

+0

你的URL說'網頁不可用'。 – 2014-11-25 07:01:11

回答

2

首先在無效

所有的JSON響應的

有效的JSON:

{"data":[{"l_id":"1","p_id":"5","user_id":"15"},{"l_id":"2","p_id":"7","user_id":"16"}],"countdata":"2"} 

如何獲得countdata:

String strCount = jsonobject.getString("countdata"); 
+0

如何在獲取strCount時在Textview上設置文本。我使用ListView與ViewHolder和我setText strCount爲每個項目,並單擊所選項目的Listview時增加1。我得到strCount成功,並點擊所選項目的listview然後增加1。但如何在Textview上設置文本? – Reena 2014-11-25 07:49:03

+0

用你的要求提出新的問題。 – 2014-11-25 07:54:30

+0

確定我發佈了新的問題在stackoverflow – Reena 2014-11-25 07:57:15

相關問題