Search code examples
androidcachinghttpurlconnectionhttpresponsecache

Using HttpResponseCache makes app crash


I'm trying to use HttpResponseCache to cache network data, but when I put install cache code to OnCreate and OnPause methods, app crashes. I used code from HttpResponseCache documentation. and compile errors appear as shown below. after using

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)

to solve errors it disappear then, I run app but still crashes. when I remove that code it returns to it's normal behavior.

protected void onCreate(Bundle savedInstanceState) {
       ...

       try {
           File httpCacheDir = new File(context.getCacheDir(), "http");
           long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
           HttpResponseCache.install(httpCacheDir, httpCacheSize); //error
        } catch (IOException e) {
           Log.i(TAG, "HTTP response cache installation failed:" + e);
       }
   }

   protected void onStop() {
       ...

       HttpResponseCache cache = HttpResponseCache.getInstalled();//error
       if (cache != null) {
           cache.flush();//error
       }
   }}

I'm a beginner learner so I'll be grateful if anyone can help. here's MainActivity code

    public class MainActivity extends AppCompatActivity  {


    private MovieAdapter movieAdapter;
    private Context context;



    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // ------ caching data ------------------
        try {
            File httpCacheDir = new File(context.getCacheDir(), "http");
            long httpCacheSize = 2 * 1024 * 1024; // 10 MiB
            HttpResponseCache.install(httpCacheDir, httpCacheSize); //error
        } catch (IOException e) {
            Log.i("caching failed", "HTTP response cache installation failed:" + e);
        }

        //-----------------------

        DataSyncTask dataSyncTask = new DataSyncTask();
        dataSyncTask.execute();
    }


        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));

            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private class DataSyncTask extends AsyncTask<Void, Void, ArrayList<Movie>> {

        @Override
        protected ArrayList<Movie> doInBackground(Void... params) {
            JSONHttpClient jsonHttpClient = new JSONHttpClient(MainActivity.this);
            JSONDataParser finalData = new JSONDataParser();
            /*
            using shared preference to obtain data
             */
            SharedPreferences sharedPreferences =
                    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

            String sortingOrder = sharedPreferences.getString(
                    getString(R.string.pref_findMovies_key),
                    getString(R.string.pref_popular_value));

            String streamData = jsonHttpClient.getData(sortingOrder);
            return finalData.getParsedData(streamData);

        }

        @Override
        protected void onPostExecute(final ArrayList<Movie> movies) {


            movieAdapter = new MovieAdapter(getApplicationContext(), movies);
            GridView gridView = (GridView) findViewById(R.id.movies_grid);
            gridView.setAdapter(movieAdapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Movie movie = (Movie) adapterView.getItemAtPosition(i);
                    Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                    intent.putExtra("MovieTitle", movie.getTitle());
                    intent.putExtra("voteAverage", movie.getVoteAverage());
                    intent.putExtra("ReleaseDate", movie.getReleaseDate());
                    intent.putExtra("MovieOverview", movie.getOverview());
                    intent.putExtra("PosterPath", movie.getPosterPath());

                    startActivity(intent);
                }
            });

        }
    }

    @Override
    protected void onResume() {
        DataSyncTask dataSyncTask = new DataSyncTask();
        dataSyncTask.execute();
        super.onResume();

    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void onStop() {
        HttpResponseCache cache = HttpResponseCache.getInstalled();
        if (cache != null) {
            cache.flush();
        }
        super.onStop();
    }
}

that what appear in logcat after app crash

12-19 19:22:58.465  17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
12-19 19:22:58.465  17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
12-19 19:22:58.465  17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve interface method 14585: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
12-19 19:22:58.465  17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
12-19 19:22:58.465  17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve interface method 14589: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
12-19 19:22:58.505  17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-19 19:22:58.505  17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve virtual method 451: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-19 19:22:58.515  17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-19 19:22:58.515  17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve virtual method 473: Landroid/content/res/TypedArray;.getType (I)I
12-19 19:22:58.585  17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415ffc80)

Solution

  • According to HttpResponseCache Docs:

    Enable caching of all of your application's HTTP requests by installing the cache at application startup.

    So you should use as the following:

    MyApplication.java:

    public class MyApplication extends Application {
    
        private final Context mContext = this;
    
        public void onCreate() {
            super.onCreate();
    
            // Install the cache
            try {
                File httpCacheDir = new File(mContext.getCacheDir(), "http");
                long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
                HttpResponseCache.install(httpCacheDir, httpCacheSize);
            } catch (IOException e) {
                Log.i("CacheHttpUrlConnection", "HTTP response cache installation failed:" + e);
            }
    
            // Register for activity lifecycle callbacks,
            // specifically interested in activity stop callbacks.
            registerActivityLifecycleCallbacks(
                    new MyApplicationActivityLifeCycleCallbacks());
        }
    
        // method to flush cache contents to the filesystem
        public void flushCache() {
            HttpResponseCache cache = HttpResponseCache.getInstalled();
            if (cache != null) {
                cache.flush();
            }
        }
    
        private class MyApplicationActivityLifeCycleCallbacks implements ActivityLifecycleCallbacks {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }
    
            @Override
            public void onActivityStarted(Activity activity) {
            }
    
            @Override
            public void onActivityResumed(Activity activity) {
            }
    
            @Override
            public void onActivityPaused(Activity activity) {
            }
    
            public void onActivityStopped(Activity activity) {
                flushCache();
            }
    
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }
    
            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        }
    }
    

    MainActivity.java:

    Inside doInBackground of AsyncTask class:

    ...
    URL url = new URL(address);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
    urlConnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
    // Checks cache
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        cache = HttpResponseCache.getInstalled();
        if (cache != null) {
            cacheInfo = "Request count: "
                    + cache.getRequestCount() + ", hit count "
                    + cache.getHitCount() + ", network count "
                    + cache.getNetworkCount() + "   size = "
                    + cache.size() + " <-----------------";
            Log.i(LOG_TAG, cacheInfo);
        }
    }
    // Gets stream
    Log.i(LOG_TAG, "RespCode: " + urlConnection.getResponseCode());
    if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
        inputStream = urlConnection.getInputStream();
    } else {
        inputStream = urlConnection.getErrorStream();
    }
    // Parses stream to string
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String temp, response = "";
    while ((temp = bufferedReader.readLine()) != null) {
        response += temp;
    }
    ...
    

    AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.cachehttpurlconnection" >
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            android:name="com.example.cachehttpurlconnection.MyApplication" >
            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>