Yuvaan Chauhan Yuvaan Chauhan - 2 months ago 13
Android Question

How to Toast "No Data Found" From JSON parsing if getting null value in Android

I am parsing JSON value from URL if i get

null
value so i want
Toast
"data not found"


my code here


@Override
protected List<Details> doInBackground(String... params) {
List<Details> result = new ArrayList<Details>();
ServiceHandler serviceHandler=new ServiceHandler(TrainshwstnActivity.this);
String u = new String(params[0]);
String JsonStr=serviceHandler.makeServiceCall(u, ServiceHandler.GET);
Log.d("Response: ", "> " + JsonStr);
if (JsonStr != null) {
try {
JSONArray arr = new JSONArray(JsonStr);
for (int i=0; i < arr.length(); i++) {
result.add(convertContact(arr.getJSONObject(i)));
}

return result;
}
catch(Throwable t) {
t.printStackTrace();
}
return null;
}
return result;
}

Answer

Add the code after doInbackground code:

protected void onPostExecute(String result) {
     if(result == null){
     Toast.makeText(getApplicationContext(),"No data found", Toast.LENGTH_LONG).show();
     }
 }