Search code examples
androidjsonandroid-calendarandroid-cursorandroid-json

Populate Custom Calendar with json data


I need to get some json data from my server that contains data about the assistence of the company employees. I'm using an example of CalendarView

I need to change the data origin from:

Cursor cursor = context.getContentResolver()
                .query(Uri.parse("content://com.android.calendar/events"),
                        new String[] { "calendar_id", "title", "description",
                                "dtstart", "dtend", "eventLocation" }, null,
                        null, null);

to

Cursor cursor = context.getContentResolver()
                .query(Uri.parse("http://www.gettford.net/comunidad/api/calendario.php"),
                        new String[] { "calendar_id", "title", "description",
                                "dtstart", "dtend", "eventLocation" }, null,
                        null, null);

Obviously the code is wrong, but i do not know how to put json inside a cursor, i know how to get the json and insert it in an array but in this case i don't know what to do.

Appreciate any help

Thanks in advance.


Solution

  • Finally i solved by changing this:

    Cursor cursor = context.getContentResolver()
                    .query(Uri.parse("content://com.android.calendar/events"),
                            new String[] { "calendar_id", "title", "description",
                                    "dtstart", "dtend", "eventLocation" }, null,
                            null, null);
    

    for this:

    class Asistencia extends AsyncTask<Void, Void, Void> {
            @Override
            protected Void doInBackground(Void... params) {
    
                nameOfEvent.clear();
                startDates.clear();
                endDates.clear();
                descriptions.clear();
    
                // Retrieve JSON Objects from the given URL address
                jsonobject = JSONfunctions
                        .getJSONfromURL("http://www.xxx.zzz");
                if (jsonobject != null && jsonobject.length() > 0) {
                    try {
                        // Locate the array name in JSON
                        jsonarray = jsonobject.getJSONArray("datos");
    
                        for (int i = 0; i < jsonarray.length(); i++) {
    
                            jsonobject = jsonarray.getJSONObject(i);
                            // Retrive JSON Objects
                            nameOfEvent.add(jsonobject.getString("title"));
                            startDates.add(jsonobject.getString("dtstart"));
                            endDates.add(jsonobject.getString("dtend"));
                        }
                    } catch (JSONException e) {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                    }
                } else {
                    // errores = "conexion";
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void args) {
    
            }
    
        }