Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public CafeEntry(
- String id, String title, String dynamicUrl, String url, String rating, String distance, String categories) {
- this.id = id;
- this.title = title;
- this.dynamicUrl = Uri.parse(dynamicUrl);
- this.url = url;
- this.rating = rating;
- this.distance = distance;
- this.categories = categories;
- }
- /**
- * Loads a raw JSON at R.raw.products and converts it into a list of CafeEntry objects
- */
- public static List<CafeEntry> initCafeEntryList(Resources resources) {
- InputStream inputStream = resources.openRawResource(R.raw.restaurants);
- Writer writer = new StringWriter();
- char[] buffer = new char[1024];
- try {
- Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
- int pointer;
- while ((pointer = reader.read(buffer)) != -1) {
- writer.write(buffer, 0, pointer);
- }
- } catch (IOException exception) {
- Log.e(TAG, "Error writing/reading from the JSON file.", exception);
- } finally {
- try {
- inputStream.close();
- } catch (IOException exception) {
- Log.e(TAG, "Error closing the input stream.", exception);
- }
- }
- String jsonCafeString = writer.toString();
- Gson gson = new Gson();
- Type cafeListType = new TypeToken<ArrayList<CafeEntry>>() {
- }.getType();
- return gson.fromJson(jsonCafeString, cafeListType);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement