-2

This is my code.

                    JSONObject jsono = new JSONObject(response);
                        JSONArray jarray = jsono.getJSONArray("restaurents");

                        if(!jarray.equals("null")) {
                            //District value is not null

                            Toast.makeText(getContext(), "not null", Toast.LENGTH_LONG).show();
                        }else {
                            Toast.makeText(getContext(), " null", Toast.LENGTH_LONG).show();
                        }

And this is my json responce.

{ "status": "success", "restaurents": null }

3 Answers3

2
if(jarray != null){
      Toast.makeText(getContext(), "not null", Toast.LENGTH_LONG).show();

}else{
      Toast.makeText(getContext(), " null", Toast.LENGTH_LONG).show();
}
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
Tarun konda
  • 1,740
  • 1
  • 11
  • 19
1

JsonArray return object and you are treating jArray as String. So you need to treat jArray as object:-

if(jarray != null ) {
    Toast.makeText(getContext(), "not null", Toast.LENGTH_LONG).show();
}else {
    Toast.makeText(getContext(), " null", Toast.LENGTH_LONG).show();
}
duggu
  • 37,851
  • 12
  • 116
  • 113
0

Currently u are checking like this

if(!jarray.equals("null"))

where it treats "null" as string so remove " "

Try like this

if(!jarray.equals(null))

or try like this

if (jarray.length() ==0 ) 

check the size of array

Anil
  • 1,605
  • 1
  • 14
  • 24