android - 文件已存在时的 IO 异常 - Gson Android
问题描述
我正在尝试解析一个名为“category.json”的本地 json 文件,但是当我调用该文件时,我得到一个“未处理的 IO 异常”错误。我也创建了 category.json 文件。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView= inflater.inflate(R.layout.content_menu, container, false);
String jsonArray="category.json";
Gson gson = new Gson();
List<Category> list = gson.fromJson(new FileReader(jsonArray), new TypeToken<List<Category>>(){}.getType());
RecyclerView myrv = (RecyclerView)rootView.findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(getActivity(),list);
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
myrv.setAdapter(myAdapter);
return rootView;
}
我什至尝试使用 AssetManager 我仍然得到同样的错误
AssetManager assetManager =getContext().getAssets();
InputStream is = assetManager.open("category.json");
解决方案
推荐阅读
- python - 在 sqlalchemy 连接输出中指定内部
- json - 如何在terraform模板文件中将字符串转换为数字
- firebase - Firebase FireStore disableNetwork 离线使用费用
- android - 我如何/在哪里将我的 .db 文件存储在 android 上
- gcc - 使用 GCC-9.2.0 (x86) 删除 CMOV 指令
- javascript - 删除条目或更新为空白(即“”)到 JSON 文件
- python - 在python中实现8位加法器
- python - R`summary`函数在python中最接近等价
- android - 有没有一种方法可以从移动 Safari 中检测本机应用程序?
- python - 如何在 panda Dataframe 中映射新的键值对