2

I have created an application which pings some webpage and sends sms in case it fails. But when the DNS is not working this application is not sending sms because the IP of the webpage ex. www.google.com is saved into the dns cache and my application is still using this ip and as a result i am not notified about the problem.

I have tried the methods below to delete this cache files but no success :

static int clearCacheFolder(final File dir, final int numDays) {

    int deletedFiles = 0;
    if (dir!= null && dir.isDirectory()) {

        try {
                for (File child:dir.listFiles()) {

                //first delete subdirectories recursively
                if (child.isDirectory()) {
                    deletedFiles += clearCacheFolder(child, numDays);
                }

                //then delete the files and subdirectories in this dir
                //only empty directories can be deleted, so subdirs have been done first
                //if (child.lastModified() < new Date().getTime() - numDays *DateUtils.DAY_IN_MILLIS) {
                    if (child.delete()) {
                        deletedFiles++;
                    }

                //}
            }
        }
        catch(Exception e) {
            Log.e(TAG, String.format("Failed to clean the cache, error %s", e.getMessage()));
        }
    }
    return deletedFiles;
}

ublic static void clearCache(final Context context, final int numDays) {
    Log.i(TAG, String.format("Starting cache prune, deleting files older than %d days",numDays));

    int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);

    Log.i(TAG, String.format("Cache pruning completed, %d files deleted", numDeletedFiles));

This code does not clear the application cache. Does anyone knows why ? Please help !

There is also another post in this forum as below :

System.setProperty( "networkaddress.cache.ttl", "0" );
System.setProperty( "networkaddress.cache.negative.ttl", "0" );

I just wanted to know if the above code will help and how can i use it in my android code ? Please let me know !

Thanks

kosa
  • 65,990
  • 13
  • 130
  • 167
Daniel
  • 21
  • 5
  • 2
    The above code is not a solution and not working when the phone is not root. Posted in case it help anyone who is facing the same problem with mine. – Daniel Feb 26 '14 at 11:24
  • Yes, as per http://stackoverflow.com/questions/9875379/check-disabling-of-dns-caching-in-android/22078550#22078550, the only references to "networkaddress.cache" are in 3rd party test code. – Jon Shemitz Feb 27 '14 at 19:36

0 Answers0