Created
October 10, 2016 10:52
-
-
Save janishar/4d1b3a46bab60d54c87f2d574c1fc641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utils { | |
private static final String TAG = "Utils"; | |
public static List<InfiniteFeedInfo> loadInfiniteFeeds(Context context){ | |
try{ | |
GsonBuilder builder = new GsonBuilder(); | |
Gson gson = builder.create(); | |
JSONArray array = new JSONArray(loadJSONFromAsset(context, "infinite_news.json")); | |
List<InfiniteFeedInfo> feedList = new ArrayList<>(); | |
for(int i=0;i<array.length();i++){ | |
InfiniteFeedInfo feed = gson.fromJson(array.getString(i), InfiniteFeedInfo.class); | |
feedList.add(feed); | |
} | |
return feedList; | |
}catch (Exception e){ | |
Log.d(TAG,"seedGames parseException " + e); | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
private static String loadJSONFromAsset(Context context, String jsonFileName) { | |
String json = null; | |
InputStream is=null; | |
try { | |
AssetManager manager = context.getAssets(); | |
Log.d(TAG,"path "+jsonFileName); | |
is = manager.open(jsonFileName); | |
int size = is.available(); | |
byte[] buffer = new byte[size]; | |
is.read(buffer); | |
is.close(); | |
json = new String(buffer, "UTF-8"); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
return null; | |
} | |
return json; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment