Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/FiveTAPIFetcher.java
Show All 13 Lines | BusTO - Backend components | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.backend; | package it.reyboz.bustorino.backend; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import android.util.Log; | import android.util.Log; | ||||
import it.reyboz.bustorino.data.GTTInfoInject; | |||||
import org.json.JSONArray; | import org.json.JSONArray; | ||||
import org.json.JSONException; | import org.json.JSONException; | ||||
import org.json.JSONObject; | import org.json.JSONObject; | ||||
import java.io.UnsupportedEncodingException; | import java.io.UnsupportedEncodingException; | ||||
import java.net.MalformedURLException; | import java.net.MalformedURLException; | ||||
import java.net.URL; | import java.net.URL; | ||||
import java.net.URLEncoder; | import java.net.URLEncoder; | ||||
▲ Show 20 Lines • Show All 207 Lines • ▼ Show 20 Lines | public List<Route> getDirectionsForStop(String stopID, AtomicReference<Result> res) { | ||||
return routes; | return routes; | ||||
} | } | ||||
public ArrayList<Stop> getAllStopsFromGTT(AtomicReference<Result> res){ | public ArrayList<Stop> getAllStopsFromGTT(AtomicReference<Result> res){ | ||||
String response = performAPIRequest(QueryType.STOPS_ALL,null,res); | String response = performAPIRequest(QueryType.STOPS_ALL,null,res); | ||||
if(response==null) return null; | if(response==null) return null; | ||||
ArrayList<Stop> stopslist; | ArrayList<Stop> stopslist; | ||||
try{ | try{ | ||||
JSONObject responseJSON = new JSONObject(response); | //JSONObject responseJSON = new JSONObject(response); | ||||
JSONArray stops = responseJSON.getJSONArray("stops"); | JSONArray stops = new JSONArray(response);//responseJSON.getJSONArray("stops"); | ||||
stopslist = new ArrayList<>(stops.length()); | stopslist = new ArrayList<>(stops.length()); | ||||
for (int i=0;i<stops.length();i++){ | for (int i=0;i<stops.length();i++){ | ||||
JSONObject currentStop = stops.getJSONObject(i); | JSONObject currentStop = stops.getJSONObject(i); | ||||
String location = currentStop.getString("location"); | String location = currentStop.getString("location"); | ||||
if(location.trim().equals("_")) location = null; | if(location.trim().equals("_")) location = null; | ||||
String placeName = currentStop.getString("placeName"); | String placeName = currentStop.getString("placeName"); | ||||
if(placeName.trim().equals("_")) placeName = null; | if(placeName.trim().equals("_")) placeName = null; | ||||
String[] lines = currentStop.getString("lines").split(","); | String[] lines = currentStop.getString("lines").split(","); | ||||
for(int l = 0; l<lines.length;l++){ | for(int l = 0; l<lines.length;l++){ | ||||
lines[l] = FiveTNormalizer.routeDisplayToInternal(lines[l]); | lines[l] = FiveTNormalizer.routeDisplayToInternal(lines[l]); | ||||
} | } | ||||
Route.Type t; | Route.Type t; | ||||
switch (currentStop.getString("type")){ | switch (currentStop.getString("type")){ | ||||
case "BUS": | case "BUS": | ||||
t = Route.Type.BUS; | t = Route.Type.BUS; | ||||
break; | break; | ||||
case "METRO": | case "METRO": | ||||
t = Route.Type.METRO; | t = Route.Type.METRO; | ||||
break; | break; | ||||
case "TRENO": | case "TRENO": | ||||
t = Route.Type.RAILWAY; | t = Route.Type.RAILWAY; | ||||
break; | break; | ||||
default: | default: | ||||
t = Route.Type.UNKNOWN; | t = Route.Type.UNKNOWN; | ||||
} | } | ||||
Stop s = new Stop(currentStop.getString("id"), | String stopName = currentStop.getString("name"); | ||||
currentStop.getString("name"),null,location,t,Arrays.asList(lines), | String stopID; | ||||
if(stopName.toLowerCase().contains("metro")) | |||||
t= Route.Type.METRO; | |||||
try { | |||||
stopID = currentStop.getString("id"); | |||||
} catch (JSONException exc){ | |||||
// we don't have the ID | |||||
//check if we have it already as hardcoded | |||||
stopID = GTTInfoInject.findIDWhenMissingByName(stopName); | |||||
if(stopID.length()==0){ | |||||
// we haven't found it, skip stop | |||||
Log.e(DEBUG_NAME, "Cannot find the ID for stop name: "+stopName); | |||||
continue; | |||||
} | |||||
} | |||||
Stop s = new Stop(stopID, stopName, | |||||
null,location,t,Arrays.asList(lines), | |||||
Double.parseDouble(currentStop.getString("lat")), | Double.parseDouble(currentStop.getString("lat")), | ||||
Double.parseDouble(currentStop.getString("lng"))); | Double.parseDouble(currentStop.getString("lng"))); | ||||
if(placeName!=null) | if(placeName!=null) | ||||
s.setAbsurdGTTPlaceName(placeName); | s.setAbsurdGTTPlaceName(placeName); | ||||
stopslist.add(s); | stopslist.add(s); | ||||
} | } | ||||
res.set(Result.OK); | res.set(Result.OK); | ||||
▲ Show 20 Lines • Show All 130 Lines • Show Last 20 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register