FarhaSameer786
FarhaSameer786

Reputation: 370

How to check the length of JSONArray

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

Can Anyone help me.this is my code where i fetch the data from server through pages like pass the numbers on next button clicked.when it is not feteching data from server how i show the message there is no data..

Upvotes: 1

Views: 6641

Answers (3)

Hemantvc
Hemantvc

Reputation: 2119

please give to json array name in your url

like -

{
    "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"
                }
        },
        .
        .
        .
        .
  ]
}

this jParser.getJSONFromUrl(url+"page="+pages) function returns a json object .

write the following code

// 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);

Upvotes: 1

Addicted Manish
Addicted Manish

Reputation: 189

do in this way... here is a async class...

class MyTask extends 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..

}

Upvotes: 0

Nizam
Nizam

Reputation: 5731

As I see in your code, your JSONArray is

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

now, immediately after that line, check its length

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

Upvotes: 0

Related Questions