Search in sources :

Example 1 with MessageUpdate

use of flying.grub.tamtime.data.update.MessageUpdate in project TamTime by flyingrub.

the class RealTimes method updateLine.

public void updateLine(Line line) {
    JSONObject data = null;
    try {
        data = lineToJSON(line);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonArrayReq req = new JsonArrayReq(Request.Method.POST, JSON_REALTIME_LINE, data, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject stop = response.getJSONObject(i);
                    int id = stop.getInt("cityway_stop_id");
                    JSONArray timesJSON = stop.getJSONArray("stop_next_time");
                    ArrayList<Time> times = new ArrayList<>();
                    for (int j = 0; j < timesJSON.length(); j++) {
                        JSONObject timeJSON = timesJSON.getJSONObject(j);
                        String waiting_time = timeJSON.getString("waiting_time");
                        int hour = timeJSON.getInt("passing_hour");
                        int min = timeJSON.getInt("passing_minute");
                        times.add(new Time(waiting_time, hour, min));
                    }
                    Data.getData().getMap().addTimeToStop(id, times);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            EventBus.getDefault().post(new MessageUpdate(MessageUpdate.Type.TIMES_UPDATE));
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "error :" + error.networkResponse);
        }
    });
    VolleyApp.getInstance(context).addToRequestQueue(req);
}
Also used : VolleyError(com.android.volley.VolleyError) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) MessageUpdate(flying.grub.tamtime.data.update.MessageUpdate) JsonArrayReq(flying.grub.tamtime.data.connection.JsonArrayReq) Response(com.android.volley.Response) JSONObject(org.json.JSONObject)

Example 2 with MessageUpdate

use of flying.grub.tamtime.data.update.MessageUpdate in project TamTime by flyingrub.

the class RealTimes method updateStops.

public void updateStops(ArrayList<Stop> stops) {
    JSONObject data = null;
    try {
        data = stopsToJSON(stops);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonArrayReq req = new JsonArrayReq(Request.Method.POST, JSON_REALTIME_STOPS, data, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject stop = response.getJSONObject(i);
                    int id = stop.getInt("cityway_stop_id");
                    JSONArray timesJSON = stop.getJSONArray("stop_next_time");
                    ArrayList<Time> times = new ArrayList<>();
                    for (int j = 0; j < timesJSON.length(); j++) {
                        JSONObject timeJSON = timesJSON.getJSONObject(j);
                        String waiting_time = timeJSON.getString("waiting_time");
                        int hour = timeJSON.getInt("passing_hour");
                        int min = timeJSON.getInt("passing_minute");
                        times.add(new Time(waiting_time, hour, min));
                    }
                    Data.getData().getMap().addTimeToStop(id, times);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            EventBus.getDefault().post(new MessageUpdate(MessageUpdate.Type.TIMES_UPDATE));
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "error :" + error);
        }
    });
    VolleyApp.getInstance(context).addToRequestQueue(req);
}
Also used : VolleyError(com.android.volley.VolleyError) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) MessageUpdate(flying.grub.tamtime.data.update.MessageUpdate) JsonArrayReq(flying.grub.tamtime.data.connection.JsonArrayReq) Response(com.android.volley.Response) JSONObject(org.json.JSONObject)

Example 3 with MessageUpdate

use of flying.grub.tamtime.data.update.MessageUpdate in project TamTime by flyingrub.

the class ReportEvent method setReport.

public void setReport(JSONArray reportJson) {
    for (Report r : this.reportList) {
        r.removeFromStop();
    }
    this.reportList = new ArrayList<>();
    try {
        for (int i = 0; i < reportJson.length(); i++) {
            JSONObject reportObjectJson = reportJson.getJSONObject(i);
            String msg = reportObjectJson.optString("message");
            int confirm = reportObjectJson.getInt("confirm");
            int reportId = reportObjectJson.getInt("id");
            Calendar date = Calendar.getInstance();
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date.setTime(sdf.parse(reportObjectJson.getString("date")));
            } catch (Exception e) {
                e.printStackTrace();
            }
            StopZone stop = Data.getData().getMap().getStopZoneById(reportObjectJson.getInt("stop"));
            if (stop != null) {
                Report report = new Report(stop, ReportType.reportFromId(reportObjectJson.getInt("type")), msg, date, confirm, reportId);
                this.reportList.add(report);
            }
        }
        EventBus.getDefault().post(new MessageUpdate(MessageUpdate.Type.REPORT_UPDATE));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) Calendar(java.util.Calendar) JSONException(org.json.JSONException) MessageUpdate(flying.grub.tamtime.data.update.MessageUpdate) SimpleDateFormat(java.text.SimpleDateFormat) StopZone(flying.grub.tamtime.data.map.StopZone) JSONException(org.json.JSONException)

Aggregations

MessageUpdate (flying.grub.tamtime.data.update.MessageUpdate)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 Response (com.android.volley.Response)2 VolleyError (com.android.volley.VolleyError)2 JsonArrayReq (flying.grub.tamtime.data.connection.JsonArrayReq)2 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 StopZone (flying.grub.tamtime.data.map.StopZone)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1