You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StopsDataAccess.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.rthoni.stssaguenay.dataaccess;
  2. import android.util.Pair;
  3. import com.rthoni.stssaguenay.dbo.LuDataAccessConfigDbo;
  4. import com.rthoni.stssaguenay.dbo.LuDbo;
  5. import com.rthoni.stssaguenay.dbo.StopsDbo;
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Locale;
  12. import java.util.Vector;
  13. /**
  14. * Created by robin on 9/29/16.
  15. */
  16. public class StopsDataAccess {
  17. public static class StopsDboRaw extends LuDbo {
  18. protected List<StopsDbo> _stops;
  19. @Override
  20. public void fromJson(JSONObject json) throws JSONException {
  21. _stops = new Vector<>();
  22. JSONArray features = json.getJSONArray("features");
  23. for (int i = 0; i < features.length(); ++i) {
  24. JSONObject obj = features.getJSONObject(i);
  25. JSONObject props = obj.getJSONObject("properties");
  26. JSONArray routes = props.getJSONArray("route_ids");
  27. List<String> routesStr = new Vector<>();
  28. for (int j = 0; j < routes.length(); ++j) {
  29. routesStr.add(routes.getString(j));
  30. }
  31. JSONArray pos = obj.getJSONObject("geometry").getJSONArray("coordinates");
  32. StopsDbo stop = new StopsDbo();
  33. stop.setId(obj.getString("id"));
  34. stop.setName(props.getString("name"));
  35. stop.setRoutes(routesStr);
  36. stop.setPosition(new Pair<Double, Double>(pos.getDouble(0), pos.getDouble(1)));
  37. _stops.add(stop);
  38. }
  39. }
  40. @Override
  41. public HashMap<String, Object> toArray() {
  42. return null;
  43. }
  44. public List<StopsDbo> getStops()
  45. {
  46. return _stops;
  47. }
  48. }
  49. public static LuPromise<List<StopsDbo>> getAll(LuDataAccessConfigDbo config, int unknownParam)
  50. {
  51. return LuRequest.get(config, StopsDboRaw.class, String.format(Locale.getDefault(), "transit/%d/week/stops.json", unknownParam))
  52. .map(new LuPromise.LuConverter<StopsDboRaw, List<StopsDbo>>() {
  53. @Override
  54. public List<StopsDbo> convert(StopsDboRaw data) {
  55. return data.getStops();
  56. }
  57. });
  58. }
  59. }