2013-07-31 74 views
0
的長度
@Override 
protected String doInBackground(String... params) { 
     try{ 
      JSONParser jParser = new JSONParser(); 
      String url="http://twominenglish.com/api/getlatest?"; 
      IdArray = jParser.getJSONFromUrl(url+"page="+pages); 

      mLatestList.clear(); 
      for(int i=0;i<IdArray.length();i++){ 
       if(IdArray.isNull(i)) 
       { 
        Toast.makeText(mContext, "No Value", Toast.LENGTH_SHORT).show(); 
       } 
       try{ 
        JSONObject jObject; 
        mLtest=new Latest(); 
        jObject=IdArray.getJSONObject(i); 

        mLtest.SetID(jObject.getString("ID")); 
        //mLtest.SetImageUrl(jObject.getString("ImageURL")); 
        String path="http://twominenglish.com"+jObject.getString("ImageURL"); 
        mLtest.SetImageUrl(path); 
        mLtest.SetDescription(jObject.getString("Description")); 
        mLtest.SetTitle(jObject.getString("Title")); 
        mLatestList.add(mLtest); 
        mLtest=new Latest(); 

       }catch(JSONException e){ 
        e.printStackTrace(); 
       } 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
} 

誰能幫me.this是我的代碼,我從服務器通過類似的頁面獲取數據傳給下一個按鈕的數字clicked.when它不是從服務器feteching數據如何我顯示消息沒有數據..如何檢查JSONArray

+1

你的問題我不清楚。 – Raghunandan

+0

您是否從服務中獲得相同的回覆? – KOTIOS

+0

你可以使用['jsonArray.length()'](http://www.json.org/javadoc/org/json/JSONArray.html#length())方法來檢查'JSONArray'的長度,再加上你可以使用android listview的emptyView選項。看看:[Android - 顯示警報對話框爲空ListView](http://stackoverflow.com/a/8265211/593709) –

回答

1

請給JSON數組名稱與URL

像 -

{ 
    "contacts": [ 
     { 
       "id": "c200", 
       "name": "Ravi Tamada", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     }, 
     { 
       "id": "c201", 
       "name": "Johnny Depp", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     }, 
     . 
     . 
     . 
     . 
    ] 
} 

這​​函數返回一個JSON對象。

寫代碼如下

// contacts JSONArray 
JSONArray IdArray = null; 

/getting JSON string from URL 
JSONObject json = jParser.getJSONFromUrl(url); 

try { 
    // Getting Array of Contacts 
    IdArray = json.getJSONArray(TAG_CONTACTS); 
+0

也許你應該指出'IdArray.length'存儲數組結果的長度 – kabuto178

0

正如我在你的代碼中看到,您的JSONArray是

IdArray = jParser.getJSONFromUrl(url+"page="+pages);

現在,這條線後,立即檢查其長度

if(IdArray.length()>0) 
{ 
// do all operations here 
} 
else{ 
Toast.makeText(mContext, "Array empty", Toast.LENGTH_SHORT).show(); 
} 
+3

這是正確的答案,不知道誰在關閉 - 投票的答案.. –

+2

也許有人不喜歡簡單的解決方案:P – Nizam

0

這樣做... 這裏是一個同步類...

類MyTask擴展的AsyncTask {

@Override 
    protected void onPreExecute() { 

     super.onPreExecute(); 
    } 


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

     String result=IOUtils.getUrlResponse(params[0]); 

     return result; 

     // TODO Auto-generated method stub 

    } 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
      JSONArray mainJson = new JSONArray(result); 
      JSONArray objjson=mainJson.getJSONObject(0).getJSONArray("data"); 

    Log.i("lenghth",objjson.length()); //this will return length of Json Array.. 

}