Advertisement
MarRab

Model json

Sep 30th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1.  
  2.     public List<Month> months = new ArrayList<Month>();
  3.  
  4.     public void saveMonths(Context context) {
  5.         Gson gson = new Gson();
  6.         String gsonOutput = gson.toJson(this.months, ArrayList.class);
  7.         FileOutputStream fos;
  8.         try {
  9.             if (context.getFileStreamPath(MONTHS_FILE).exists())
  10.                 context.deleteFile(MONTHS_FILE);
  11.             fos = context.openFileOutput(MONTHS_FILE, Context.MODE_PRIVATE);
  12.             fos.write(gsonOutput.getBytes());
  13.             fos.close();
  14.         } catch (FileNotFoundException e) {
  15.             Log.e(e);
  16.         } catch (IOException e) {
  17.             Log.e(e);
  18.         }
  19.     }
  20.  
  21.     public void restoreMonths(Context context) {
  22.         Gson gson = new Gson();
  23.         try {
  24.             this.months = gson.fromJson(new InputStreamReader(context.openFileInput(MONTHS_FILE)), new TypeToken<List<Month>>() {
  25.             }.getType());
  26.         } catch (JsonSyntaxException e) {
  27.             Log.e(e);
  28.         } catch (JsonIOException e) {
  29.             Log.e(e);
  30.         } catch (FileNotFoundException e) {
  31.             Log.e(e);
  32.         }
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement