0

I'm trying to get a JSON response from a server, then parse it, and add some date events to calendar of Material-Calendar-View plugin.

And it's almost working, but the date decoration to calendar added, not at once, but after scrolling a view pages in calendar.

I have an idea that I should put all my code to another thread, because have an exception

Skipped 31 frames! The application may be doing too much work on its main thread.

But i'm not sure should I, and if I should, I don't know how to make it right.

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        events = new ArrayList<>();
        calendarView = (CalendarView) getView().findViewById(R.id.calendarView);
        mRequestQueue = VolleySingleton.getInstance(getContext()).getRequestQueue();
        JSONParse();
        calendarView.setEvents(events);
    }
    private void JSONParse() {
        String url = "https://api.myjson.com/bins/14htbv";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                response -> {
                    try {
                        jsonArray = response.getJSONArray("info_stories");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject hit = jsonArray.getJSONObject(i);
                            String dateStr = hit.getString("date");
                            String text_main = hit.getString("text_main");

                            Date date = sdf.parse(dateStr);
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(date);
                            events.add(new EventDay(cal, R.drawable.ic_home));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }, error -> {
                    error.printStackTrace();
                });
        mRequestQueue.add(request);
    }
Vall0n
  • 1,601
  • 2
  • 14
  • 19
  • 1
    before you go with your code, just check [this](https://stackoverflow.com/a/32295725/5110595) – Hemant Parmar Jun 07 '19 at 11:20
  • I don't know the `Material-Calendar-View` library. But after adding and new Event you have to update also the calendar view. Because by the time you set the events to the calendarView the list of events is empty since the response for the request is not finished. And for the `Skipping frames stuff` see the comment from HemantParmar... – Vall0n Jun 07 '19 at 11:24
  • @Vall0n, yep it works, thanks) And also thanks for editing post) – Андрей Боровилов Jun 07 '19 at 11:39

1 Answers1

0

That solved the problem)

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    events = new ArrayList<>();
    calendarView = (CalendarView) getView().findViewById(R.id.calendarView);
    mRequestQueue = VolleySingleton.getInstance(getContext()).getRequestQueue();
    JSONParse();
}
private void JSONParse() {
    String url = "https://api.myjson.com/bins/14htbv";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            response -> {
                try {
                    jsonArray = response.getJSONArray("info_stories");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject hit = jsonArray.getJSONObject(i);
                        String dateStr = hit.getString("date");
                        String text_main = hit.getString("text_main");

                        Date date = sdf.parse(dateStr);
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(date);
                        events.add(new EventDay(cal, R.drawable.ic_home));
                        calendarView.setEvents(events);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }, error -> {
                error.printStackTrace();
            });
    mRequestQueue.add(request);
}
  • I would probably call `calendarView.setEvents(events);` outside of the for loop. So collect all events and once the list is complete set the events. – Vall0n Jun 07 '19 at 12:09