0

I'm trying to parse a local json file called "category.json" but when I call the file I get an "Unhandled IO Exception" error. I have created the category.json file as well.

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;
}

I even tried using the AssetManager I still get the same error

    AssetManager assetManager =getContext().getAssets();
    InputStream is = assetManager.open("category.json");
Community
  • 1
  • 1

1 Answers1

0

You have to handle the IOException using a try - catch as follows,

try {
    AssetManager assetManager = getContext().getAssets();
    InputStream is = assetManager.open("category.json");

    // do something with your stream

} catch (IOException e) {
    e.printStackTrace();
}

Read more about exception handling here.

Similar thread here.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
  • I tried handling it. But that doesn’t read the file. So i tried directly running it instead. The problem is why it doesn’t read the json file – Kyle Barnabus May 14 '18 at 06:06