title: Need an example of HttpResponseCache in Android
body: <p>Hi I am trying to use the HttpResponseCache introduced in Android 4.The docs do talk clearly about how to install the cache but I am at a complete loss on how to cache Images downloaded from the net.Earlier I was using the DiskLruCache to cache them. Would anyone point me towards some examples of working code where HttpResponseCache has been used..</p>
  
  <p>Edit:- Can someone tell me what I am doing wrong here:-</p>
  
  <pre><code>MainActivity.java
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
      final File httpCacheDir = new File(getCacheDir(), "http");
      try {
          Class.forName("android.net.http.HttpResponseCache")
              .getMethod("install", File.class, long.class)
              .invoke(null, httpCacheDir, httpCacheSize);
          Log.v(TAG,"cache set up");
      } catch (Exception httpResponseCacheNotAvailable) {
          Log.v(TAG, "android.net.http.HttpResponseCache not available, probably because we're running on a pre-ICS version of Android. Using com.integralblue.httpresponsecache.HttpHttpResponseCache.");
          try{
              com.integralblue.httpresponsecache.HttpResponseCache.install(httpCacheDir, httpCacheSize);
          }catch(Exception e){
              Log.v(TAG, "Failed to set up com.integralblue.httpresponsecache.HttpResponseCache");
          }
      }
      TheMainListFrag gf=(TheMainListFrag) getSupportFragmentManager().findFragmentByTag("thelistfrags");
      if(gf==null){
          gf=TheMainListFrag.newInstance();
          FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
          ft.replace(R.id.thelefty, gf,"thelistfrags");
          ft.commit();
      }
  }
  </code></pre>
  
  <p>Then in the loader of TheMainListFrag, I do the below:-</p>
  
  <pre><code>public ArrayList&lt;HashMap&lt;String,String&gt;&gt; loadInBackground() {
      String datafromServer = null;
      ArrayList&lt;HashMap&lt;String,String&gt;&gt; al = new ArrayList&lt;HashMap&lt;String,String&gt;&gt;();
      try {
          String url = "someurl";
          HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
  
          urlConnection.setRequestProperty("Accept", "application/json");
          InputStream is = new BufferedInputStream(urlConnection.getInputStream());
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = br.readLine()) != null) {
              sb.append(line);
          }
          datafromServer=sb.toString();
          Log.v("fromthread",datafromServer);
          // etc 
                      //etc
  
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (Exception e) {
          Log.v("fromthread", e.getClass() + "--" + e.getMessage());
      }
  
      return al;
  }
  </code></pre>
  
  <p>When i am connected to internet, it works fine, and in the directory http-the cache directory named above, i can see the files too. But when I am not connected to the internet, the data refuses to load.</p>
  
  <p>When i load images from the net, i see the cache files named as .tmp , which i believe are termed as dirty as per DiskLruCache.</p>
  
  <p>Please let me know if there is any other info that you want me to provide</p>