/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 237 by torben, Sat Aug 8 19:02:20 2009 UTC revision 244 by torben, Sun Aug 9 17:46:09 2009 UTC
# Line 1  Line 1 
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    
# Line 21  import org.xml.sax.helpers.DefaultHandle Line 15  import org.xml.sax.helpers.DefaultHandle
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    
# Line 34  public class XmlDepartureProvider extend Line 29  public class XmlDepartureProvider extend
29          public void lookupDepartures(String station) {          public void lookupDepartures(String station) {
30                  departures.clear();                  departures.clear();
31                  try                  try
32                  {                        {       String url = "http://t-hoerup.dk/tog/xml_display.php?stationname="+station;
33                          String doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station);                          String doc =  DownloadUtil.getContent(url, 30000, "ISO-8859-1");
34                                                    
35                          InputSource source = new InputSource( new StringReader(doc));                          InputSource source = new InputSource( new StringReader(doc));
36                                                    
# Line 56  public class XmlDepartureProvider extend Line 51  public class XmlDepartureProvider extend
51          public List<DepartureBean> getDepartures() {          public List<DepartureBean> getDepartures() {
52                  return departures;                  return departures;
53          }          }
   
         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();  
         }  
54                    
55          // 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
56          @Override          @Override
# Line 100  public class XmlDepartureProvider extend Line 75  public class XmlDepartureProvider extend
75                  if (name.equals("train")) {                  if (name.equals("train")) {
76                          departures.add( tempDeparture );                          departures.add( tempDeparture );
77                  } else if (name.equals("time")) {                  } else if (name.equals("time")) {
78                          tempDeparture.setTime(builder.toString());                          tempDeparture.setTime(builder.toString().trim());
79                  } else if (name.equals("updated")) {                  } else if (name.equals("updated")) {
80                          tempDeparture.setLastUpdate(builder.toString());                          tempDeparture.setLastUpdate(builder.toString().trim());
81                  } else if (name.equals("trainnumber")) {                  } else if (name.equals("trainnumber")) {
82                          tempDeparture.setTrainNumber(builder.toString());                          tempDeparture.setTrainNumber(builder.toString().trim());
83                  } else if (name.equals("destination")) {                  } else if (name.equals("destination")) {
84                          tempDeparture.setDestination(builder.toString());                          tempDeparture.setDestination(builder.toString().trim());
85                  } else if (name.equals("origin")) {                  } else if (name.equals("origin")) {
86                          tempDeparture.setOrigin(builder.toString());                          tempDeparture.setOrigin(builder.toString().trim());
87                  } else if (name.equals("location")) {                  } else if (name.equals("location")) {
88                          tempDeparture.setLocation(builder.toString());                          tempDeparture.setLocation(builder.toString().trim());
89                  } else if (name.equals("status")) {                  } else if (name.equals("status")) {
90                          tempDeparture.setStatus(builder.toString());                          tempDeparture.setStatus(builder.toString().trim());
91                  } else if (name.equals("note")) {                  } else if (name.equals("note")) {
92                          tempDeparture.setNote(builder.toString());                          tempDeparture.setNote(builder.toString().trim());
93                  }                  }
94          }          }
95  }  }

Legend:
Removed from v.237  
changed lines
  Added in v.244

  ViewVC Help
Powered by ViewVC 1.1.20