[Android traffic] Use caching to avoid duplicate downloads

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/kesenhoo/article/details/7395817

redundant Downloads is redundant[duplicate download is redundant]
    • The most basic way to reduce the download is to download only those you need [Americans ' minds or more directly, though it may seem funny, but it makes sense]. From a data point of view, we can make a query for some data by passing parameters like the last update time.
    • Similarly, when downloading images, it is better for the server to reduce the size of the image, rather than let us download the full size of the picture.
cache files locally [caching file to local]
    • It is important to avoid downloading duplicate data. You can use the caching mechanism to handle this problem. Caches a static resource, such as a full picture. These cached resources need to be stored separately.
    • To ensure that the app doesn't display old data because it's cached, get the latest data from the cache and prompt for a refresh when the data expires.
  1.  long  currenttime =    System.currenttimemillis ());    HttpURLConnection Conn  = (HttpURLConnection) url.openconnection ();  long  expires = conn.getheaderfielddate ("Expires" , currenttime);  long  lastmodified = conn.getheaderfielddate ("last-modified" , currenttime);    Setdataexpirationdate (expires);  if  (lastmodified < LastUpdateTime) { /span>//  Skip update } else   { //  Parse update } 
    • Using this method, you can effectively guarantee that the cache is always up-to-date data.
    • You can use the following methods to get the external cached directory:
Context.  Getexternalcachedir();  
    • Here's how to get the internal cache, note that the data stored in memory may be purged due to insufficient internal space.
Context.  GetCache();  
    • Regardless of where the files are stored will be erased when the app is uninstalled.
Use the HttpURLConnection Response cache[using httpurlconnection Response cache]
    • In Android 4.0 added a response cache for httpurlconnection, we can use the reflection mechanism to open the HTTP response cache, see the following example:
  1. Private voidEnablehttpresponsecache () {Try {      LongHttpcachesize = 10 * 1024 * 1024;//Ten MiBFile Httpcachedir =NewFile (Getcachedir (), "http"); Class.forName ("Android.net.http.HttpResponseCache"). GetMethod ("Install", File.class,Long.class). Invoke (NULL, Httpcachedir, httpcachesize); } Catch(Exception httpresponsecachenotavailable) {log.d (TAG,"HTTP response cache is unavailable."); }  }  
    • The above sample code will open the response cache on Android over 4.0 devices without affecting the previous program.
    • After the cache is turned on, all HTTP requests in the cache can respond directly to local storage and do not require a new network connection to be opened.
    • The cache-response can be ensured by the server that it has not expired, thus reducing bandwidth. Response that are not cached will be stored in the response cache for convenience of the next request.
  PostScript: The cache mechanism is used in many practical projects, the actual operation will be much more complex, there is a chance to share a cache instance.  

Learn from: http://developer.android.com/training/efficient-downloads/redundant_redundant.html, thank you!

Reprint Please specify source:Http://blog.csdn.net/kesenhoo, thank you!

[Android traffic] Use caching to avoid duplicate downloads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.