下面我正在尝试创建一个列表,但是无法解析符号“Feed”。我遗漏了什么应该包括在内。这个想法是从我创建的文件中获取新闻文章。
public class Utils {
private static final String TAG = "Utils";
public static List<Feed> loadFeeds(Context context){
try{
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
JSONArray array = new JSONArray(loadJSONFromAsset(context, "news.json"));
List<Feed> feedList = new ArrayList<>();
for(int i=0;i<array.length();i++){
Feed feed = gson.fromJson(array.getString(i), Feed.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;
}
}
*
要创建List<Feed>
,您必须创建类Feed
并将其导入您的Utils
类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句