Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/GTTJSONFetcher.java
Show All 12 Lines | BusTO (backend components) | ||||
GNU General Public License for more details. | GNU General Public License for more details. | ||||
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 android.util.Log; | |||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
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.net.URL; | import java.net.URL; | ||||
import java.net.URLEncoder; | import java.net.URLEncoder; | ||||
import java.util.HashMap; | |||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
public class GTTJSONFetcher implements ArrivalsFetcher { | public class GTTJSONFetcher implements ArrivalsFetcher { | ||||
private final String DEBUG_TAG = "GTTJSONFetcher-BusTO"; | |||||
@Override @NonNull | @Override @NonNull | ||||
public Palina ReadArrivalTimesAll(String stopID, AtomicReference<result> res) { | public Palina ReadArrivalTimesAll(String stopID, AtomicReference<result> res) { | ||||
URL url; | URL url; | ||||
Palina p = new Palina(stopID); | Palina p = new Palina(stopID); | ||||
String routename; | String routename; | ||||
String bacino; | String bacino; | ||||
String content; | String content; | ||||
JSONArray json; | JSONArray json; | ||||
int howManyRoutes, howManyPassaggi, i, j, pos; // il misto inglese-italiano è un po' ridicolo ma tanto vale... | int howManyRoutes, howManyPassaggi, i, j, pos; // il misto inglese-italiano è un po' ridicolo ma tanto vale... | ||||
JSONObject thisroute; | JSONObject thisroute; | ||||
JSONArray passaggi; | JSONArray passaggi; | ||||
try { | try { | ||||
url = new URL("http://www.gtt.to.it/cms/index.php?option=com_gtt&task=palina.getTransitiOld&palina=" + URLEncoder.encode(stopID, "utf-8") + "&realtime=true"); | url = new URL("https://www.gtt.to.it/cms/index.php?option=com_gtt&task=palina.getTransitiOld&palina=" + URLEncoder.encode(stopID, "utf-8") + "&bacino=U&realtime=true&get_param=value"); | ||||
valerio.bozzolanUnsubmitted Not Done Inline Actionsvalerio.bozzolan: asd | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
res.set(result.PARSER_ERROR); | res.set(result.PARSER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
HashMap<String, String> headers = new HashMap<>(); | |||||
//headers.put("Referer","https://www.gtt.to.it/cms/percorari/urbano?view=percorsi&bacino=U&linea=15&Regol=GE"); | |||||
headers.put("Host", "www.gtt.to.it"); | |||||
content = networkTools.queryURL(url, res); | content = networkTools.queryURL(url, res, headers); | ||||
if(content == null) { | if(content == null) { | ||||
Log.w("GTTJSONFetcher", "NULL CONTENT"); | |||||
return p; | return p; | ||||
} | } | ||||
try { | try { | ||||
json = new JSONArray(content); | json = new JSONArray(content); | ||||
} catch(JSONException e) { | } catch(JSONException e) { | ||||
Log.w(DEBUG_TAG, "Error parsing JSON: \n"+content); | |||||
Log.w(DEBUG_TAG, e); | |||||
res.set(result.PARSER_ERROR); | res.set(result.PARSER_ERROR); | ||||
return p; | return p; | ||||
} | } | ||||
try { | try { | ||||
// returns [{"PassaggiRT":[],"Passaggi":[]}] for non existing stops! | // returns [{"PassaggiRT":[],"Passaggi":[]}] for non existing stops! | ||||
json.getJSONObject(0).getString("Linea"); // if we can get this, then there's something useful in the array. | json.getJSONObject(0).getString("Linea"); // if we can get this, then there's something useful in the array. | ||||
} catch(JSONException e) { | } catch(JSONException e) { | ||||
Log.w(DEBUG_TAG, "No existing lines"); | |||||
res.set(result.EMPTY_RESULT_SET); | res.set(result.EMPTY_RESULT_SET); | ||||
return p; | return p; | ||||
} | } | ||||
howManyRoutes = json.length(); | howManyRoutes = json.length(); | ||||
if(howManyRoutes == 0) { | if(howManyRoutes == 0) { | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(result.EMPTY_RESULT_SET); | ||||
return p; | return p; | ||||
▲ Show 20 Lines • Show All 46 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