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