
(Optional, offline mode only) Caching is necessary for an application to work in offline mode. Use the Cache API to cache and persist data when the mobile device is out of the network range. The data is processed later when the device is within the range of the network.
at the Android Developer Web site
http://developer.android.com/index.html
.The Cache API is divided into two parts, one that stores a copy of the entries you receive from the server, which is called a "server copy, " and one that stores locally modified entries, which is called a "local copy."
Use the Caching interface, which is implemented by the Cache class, to create an object to work with.
To initialize all the elements required for a cache to work, use the initializeCache method:
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache.initializeCache();
}
catch(CacheException e){
Log.e("",e.getMessage();
}
You can cache data as either persistent, which means it is stored in a database, or as nonpersistent, which means it is stored in memory. By default, cache data is persistent.
cache.setIsPersistable(false);
For all operations to work after a successful online GET request for a URL at the initial stagemergeEntries allows you to incrementally update the cache for server entries with the latest state of server objects. To enable the server cache to be in sync with the back-end service, ensure that every online HTTP GET operation on a URL is followed by a MergeEntries method. To avoid redundancies, optimize the GET operation with delta support.
try{
ILogger logger = new Logger();
Parser parser = new Parser(pref, logger);
ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
IODataFeed feed = parser.parseODataFeed(entriesdoc, collectionID, schema);
cache.MergeEntries(feed, EntryURL);parameters.
}
catch(CacheException e){
Log.e("",e.getMessage();
}
You can read entries from the cache method either locally, or from the server.
The ODataEntry class of the parser has two member properties. The getIsLocal() property checks if the particular entry is local, and getCachestate () checks if the local entry is created, updated, or deleted on the client. The cache states are represented by the cacheState{Updated,Deleted,Inserted} enum.
To show the latest version of data in your application, combine server and local entries:
/*Read entry from server.*/
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache.initializeCache();
List<IODataEntry> entries = cache.ReadEntriesServer(urlKey); // return all entries respective to the parameter.
}
catch(CacheException e){
Log.e("",e.getMessage();
}
/*Read entry from local*/
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
List<IODataEntry> entries = cache.ReadEntriesLocal(CollectionID,EntryID); // return all entries respective to either of the parameters.
}
catch(CacheException e){
Log.e("",e.getMessage();
}
You can manage entries in the local cache.
try{
//Invoke the HTTP POST request
ODataEntry entry = new ODataEntry();
entry.putPropertyValue("<property_name>", "<property_value>");
String postbody = Parser.buildODataEntryRequestBody(entry, "<collection_ID>", schema, IParser.FORMAT_XML);
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
String generated_id =cache.addEntry(entry);
//Invoke the HTTP POST request
BaseRequest post = new BaseRequest();
post.setRequestMethod(BaseRequest.REQUEST_METHOD_POST);
post.setData(postbody.getBytes());
reqMan.makeRequest(post);
}
catch(CacheException e){
Log.e("",e.getMessage();
}
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
cache.updateEntry(entry);
}
catch(CacheException e){
Log.e("",e.getMessage();
}
The delete tombstones for delta query support are not available on OData JSON format.
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
cache.deleteEntry(EntryID);
}
catch(CacheException e){
Log.e("",e.getMessage();
}
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
cache.clearLocalEntry(<EntryID>);
}
catch(CacheException e){
Log.e("",e.getMessage();
//Service document
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
cache.storeDocument(serviceDocumentObject, DocumentType.ServiceDocument, serviceDocUrl);
}
catch(CacheException e){
Log.e("",e.getMessage();
}
//MetaData Document
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache.initializeCache();
cache.storeDocument(serviceDocumentObject, DocumentType.MetaDocument, serviceDocUrl);
}
catch(CacheException e){
Log.e("",e.getMessage();
}To read the service and metadata documents from cache, use the readDocumentForUrlKey method. This method returns the stored document in the cache. The application should pass the document type and the URL for which the document is relevant.
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache.initializeCache();
IODataServiceDocument serviceDocumentObject = (IODataServiceDocument )cache.readDocument(DocumentType.ServiceDocument, serviceDocUrl);
IODataSchema metadata = (IODataSchema)cache.readDocument(DocumentType.MetaDocument, serviceDocUrl);
}
catch(CacheException e){
Log.e("",e.getMessage();
}
Use the clearCache method to clear the cache and persistence of all the entries based on the URL key. This method also deletes the delta token of any document that is stored against this URL. Use this method only when the application does not support delta queries.
try{
ILogger logger = new Logger();
ICache cache =new Cache(getApplicationContext(),logger);
cache.initializeCache();
cache.clearCache(URLKey);
}
catch(CacheException e){
Log.e("",e.getMessage();
}
The delta link retrieves the delta changes for a URL, since the last time the URL was called.
The default option for a cache is to get the delta link if the OData entry supports it. Once the mergeEntries API is invoked to save server entries into cache, the next GET fired by the application is for the delta link for the OData entry. Only delta changes are available, as the response and the application developer can call mergeEntries to merge the entries in the cache.
The application developer can disable default delta link handling by calling
requestObject.disableDeltaHandling(true);