1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
import java.io.BufferedReader; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
|
import java.io.InputStreamReader; |
|
3 |
import java.io.StringReader; |
import java.io.StringReader; |
|
import java.net.URL; |
|
|
import java.net.URLConnection; |
|
4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
|
15 |
|
|
16 |
import android.util.Log; |
import android.util.Log; |
17 |
import dk.thoerup.traininfo.DepartureBean; |
import dk.thoerup.traininfo.DepartureBean; |
18 |
|
import dk.thoerup.traininfo.util.DownloadUtil; |
19 |
|
|
20 |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
21 |
|
|
26 |
StringBuilder builder = new StringBuilder(512); |
StringBuilder builder = new StringBuilder(512); |
27 |
|
|
28 |
@Override |
@Override |
29 |
public void lookupDepartures(String station) { |
public void lookupDepartures(int stationID) { |
30 |
departures.clear(); |
departures.clear(); |
31 |
try |
try |
32 |
{ |
{ |
33 |
String doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station); |
//String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode; |
34 |
|
String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID; |
35 |
|
Log.i("xmlurl",url); |
36 |
|
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
37 |
|
|
38 |
InputSource source = new InputSource( new StringReader(doc)); |
InputSource source = new InputSource( new StringReader(doc)); |
39 |
|
|
54 |
public List<DepartureBean> getDepartures() { |
public List<DepartureBean> getDepartures() { |
55 |
return departures; |
return departures; |
56 |
} |
} |
|
|
|
|
private String getUrlContents(String uri) throws IOException |
|
|
{ |
|
|
URL url = new URL(uri); |
|
|
URLConnection conn = url.openConnection(); |
|
|
conn.setConnectTimeout(5000); |
|
|
InputStream stream = conn.getInputStream(); |
|
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"),8192); |
|
|
|
|
|
StringBuilder sbuilder = new StringBuilder(); |
|
|
|
|
|
String line; |
|
|
while ( (line = in.readLine()) != null) { |
|
|
sbuilder.append(line); |
|
|
sbuilder.append("\r\n"); |
|
|
} |
|
|
|
|
|
return sbuilder.toString(); |
|
|
} |
|
57 |
|
|
58 |
// this can be called several times fore the same text-node if there are many chardata / lines |
// this can be called several times fore the same text-node if there are many chardata / lines |
59 |
@Override |
@Override |
78 |
if (name.equals("train")) { |
if (name.equals("train")) { |
79 |
departures.add( tempDeparture ); |
departures.add( tempDeparture ); |
80 |
} else if (name.equals("time")) { |
} else if (name.equals("time")) { |
81 |
tempDeparture.setTime(builder.toString()); |
tempDeparture.setTime(builder.toString().trim()); |
82 |
} else if (name.equals("updated")) { |
} else if (name.equals("updated")) { |
83 |
tempDeparture.setLastUpdate(builder.toString()); |
tempDeparture.setLastUpdate(builder.toString().trim()); |
84 |
} else if (name.equals("trainnumber")) { |
} else if (name.equals("trainnumber")) { |
85 |
tempDeparture.setTrainNumber(builder.toString()); |
tempDeparture.setTrainNumber(builder.toString().trim()); |
86 |
} else if (name.equals("destination")) { |
} else if (name.equals("destination")) { |
87 |
tempDeparture.setDestination(builder.toString()); |
tempDeparture.setDestination(builder.toString().trim()); |
88 |
} else if (name.equals("origin")) { |
} else if (name.equals("origin")) { |
89 |
tempDeparture.setOrigin(builder.toString()); |
tempDeparture.setOrigin(builder.toString().trim()); |
90 |
} else if (name.equals("location")) { |
} else if (name.equals("location")) { |
91 |
tempDeparture.setLocation(builder.toString()); |
tempDeparture.setLocation(builder.toString().trim()); |
92 |
} else if (name.equals("status")) { |
} else if (name.equals("status")) { |
93 |
tempDeparture.setStatus(builder.toString()); |
tempDeparture.setStatus(builder.toString().trim()); |
94 |
} else if (name.equals("note")) { |
} else if (name.equals("note")) { |
95 |
tempDeparture.setNote(builder.toString()); |
tempDeparture.setNote(builder.toString().trim()); |
96 |
} |
} |
97 |
} |
} |
98 |
} |
} |