2011-08-18 70 views
1

Android提供了一些保存應用程序信息的方法。 http://developer.android.com/guide/topics/data/data-storage.html在android中保存結構化數據

現在,我的問題是,我有一些結構化的數據與任務相比,具有類別和一些其他相關的設置。所有內容都是從Web服務中提取的,必須保存以獲得性能和脫機使用。

這將是在Android上使用的最佳選擇?我認爲這個選擇實際上是SQLite數據庫或內部存儲之間的選擇。性能和實施選擇其中一個選項我會期待什麼?

回答

0

一個非常簡單的方法來緩存數據,只是你的序列化對象,並將它們保存到內部緩存e.g:

// Example object which implements Serializable 
Example example = new Example(); 

// Save an object 
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl")); 
out.writeObject((Example) example); 
out.close(); 


// Load in an object 
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl"))); 
Example example_loaded = (Example) in.readObject(); 
in.close(); 
0

如果您的Web服務爲您提供XML或JSON,您可以將其保存在文件中,然後從文件中讀取它。這就像「緩存」數據。