Guest User

Untitled

a guest
Apr 18th, 2019
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public CafeEntry(
  2. String id, String title, String dynamicUrl, String url, String rating, String distance, String categories) {
  3. this.id = id;
  4. this.title = title;
  5. this.dynamicUrl = Uri.parse(dynamicUrl);
  6. this.url = url;
  7. this.rating = rating;
  8. this.distance = distance;
  9. this.categories = categories;
  10. }
  11.  
  12. /**
  13. * Loads a raw JSON at R.raw.products and converts it into a list of CafeEntry objects
  14. */
  15. public static List<CafeEntry> initCafeEntryList(Resources resources) {
  16. InputStream inputStream = resources.openRawResource(R.raw.restaurants);
  17. Writer writer = new StringWriter();
  18. char[] buffer = new char[1024];
  19. try {
  20. Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  21. int pointer;
  22. while ((pointer = reader.read(buffer)) != -1) {
  23. writer.write(buffer, 0, pointer);
  24. }
  25. } catch (IOException exception) {
  26. Log.e(TAG, "Error writing/reading from the JSON file.", exception);
  27. } finally {
  28. try {
  29. inputStream.close();
  30. } catch (IOException exception) {
  31. Log.e(TAG, "Error closing the input stream.", exception);
  32. }
  33. }
  34. String jsonCafeString = writer.toString();
  35. Gson gson = new Gson();
  36. Type cafeListType = new TypeToken<ArrayList<CafeEntry>>() {
  37. }.getType();
  38. return gson.fromJson(jsonCafeString, cafeListType);
  39. }
RAW Paste Data