简体   繁体   中英

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;
        }

Add the code after doInbackground code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM