2014-10-04 30 views
2

我正在構建一個閱讀TechChurn等android應用程序的文章。我以json的形式從服務器獲取數據。如何在android中執行緩存?

我從json解析Id(唯一),標題,作者名稱和文章內容並在列表視圖中顯示它。

這些解析的內容存儲在本地,用於訪問沒有互聯網訪問。

這我已經完成了使用緩存功能。 這裏是我的代碼,該代碼使用緩存 -

public final class CacheThis { 
private CacheThis() { 

} 

public static void writeObject(Context context, String fileName, 
     Object object) throws IOException { 
    FileOutputStream fos; 
    ObjectOutputStream oos; 
    if (fileExistance(fileName, context)) { 
     fos = context.openFileOutput(fileName, Context.MODE_PRIVATE 
       | Context.MODE_APPEND); 
     oos = new AppendingObjectOutputStream(fos); 
    } else { 
     fos = context.openFileOutput(fileName, Context.MODE_PRIVATE 
       | Context.MODE_APPEND); 
     oos = new ObjectOutputStream(fos); 
    } 
    oos.writeObject(object); 
    oos.flush(); 
    oos.close(); 

    fos.close(); 
} 

public static List<Object> readObject(Context context, String fileName) { 
    List<Object> list = new ArrayList<Object>(0); 
    FileInputStream fis; 
    try { 

     fis = context.openFileInput(fileName); 

     ObjectInputStream ois = new ObjectInputStream(fis); 
     Object object; 
     try { 
      while (true) { 
       object = ois.readObject(); 
       list.add(object); 
      } 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     fis.close(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return list; 
} 

public static boolean fileExistance(String fname, Context context) { 
    File file = context.getFileStreamPath(fname); 
    return file.exists(); 
} 

} 

我的文章應該基於ID而不是其被加載時,應用程序啓動

+0

您可以將dson中的json字符串以其ID作爲主鍵編寫。或者,您可以將json字符串寫入一個文件中,其ID爲名稱。您可以在需要時檢索json字符串,然後解析它。如果沒有顯示特定的ID,那麼您可以點擊服務器進行更新。 – Sripathi 2014-10-04 05:35:55

+0

謝謝!你能否簡單描述一下如何在沒有dbase的情況下做到這一點? – 2014-10-04 05:38:18

+0

檢查我的答案..否則你可以嘗試Volley Library。 – Sripathi 2014-10-04 05:57:52

回答

1

使用以下方法來存儲和檢索的每次緩存數據..在這裏你可以存儲對象..

private void writeData(Object data, String fileName) { 
     try { 
      FileOutputStream fos = context.openFileOutput(fileName, 
        Context.MODE_PRIVATE); 
      ObjectOutputStream os = new ObjectOutputStream(fos); 
      os.writeObject(data); 
      os.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

public Object readData(String fileName){ 
     Object data = null; 
     if (context != null) { 
      try { 
       FileInputStream fis = context.openFileInput(fileName); 
       ObjectInputStream is = new ObjectInputStream(fis); 
       data = is.readObject(); 
       is.close(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (StreamCorruptedException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 
     return data; 
    } 

一旦你得到了服務器的響應(第一次請求到服務器)寫數據。使用該id作爲文件名。之後,檢查特定的文件,然後再打你的數據。如果該文件可用,則可以從該文件獲取數據,否則請訪問該服務器。

+0

我的應用程序通過使用您的代碼崩潰! – 2014-10-04 06:35:59

+0

您可以請告訴如何通過使用您的代碼讀取數據,因爲我正在使用對象來讀取和寫入數據。 – 2014-10-04 06:37:34

+1

如果您使用Volley,請參考緩存請求。 第9章處理排除緩存 http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ – Sripathi 2014-10-04 09:24:42