/[projects]/android/Side9/src/dk/thoerup/side9/Side9Xml.java
ViewVC logotype

Annotation of /android/Side9/src/dk/thoerup/side9/Side9Xml.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 703 - (hide annotations) (download)
Mon May 3 14:54:30 2010 UTC (14 years ago) by torben
File size: 2942 byte(s)
Experimental force update thingie
1 torben 632 package dk.thoerup.side9;
2    
3     import java.io.ByteArrayInputStream;
4     import java.io.ByteArrayOutputStream;
5     import java.io.IOException;
6     import java.io.InputStream;
7     import java.net.URL;
8     import java.net.URLConnection;
9    
10     import javax.xml.parsers.DocumentBuilder;
11     import javax.xml.parsers.DocumentBuilderFactory;
12     import javax.xml.parsers.ParserConfigurationException;
13    
14     import org.w3c.dom.Document;
15     import org.w3c.dom.Node;
16     import org.w3c.dom.NodeList;
17     import org.xml.sax.SAXException;
18    
19 torben 633 import android.util.Log;
20 torben 632
21    
22 torben 633
23 torben 632 public class Side9Xml {
24    
25 torben 649 static final int WIDTH = 400;
26 torben 632 static final String BASEURL = "http://apps.todic.net/side9/";
27    
28     public static Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException
29     {
30     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
31     return builder.parse( new ByteArrayInputStream(str.getBytes()) );
32     }
33    
34    
35     public static byte[] getContent(String uri) throws IOException {
36     byte buffer[] = new byte[256];
37    
38     ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes
39    
40     URL url = new URL(uri);
41     URLConnection connection = url.openConnection();
42 torben 703 connection.setConnectTimeout(2500);
43 torben 632 InputStream is = connection.getInputStream();
44    
45     try {
46     int count;
47     while ( (count = is.read(buffer)) != -1) {
48     baos.write(buffer, 0, count);
49     }
50     } finally {
51     try {
52     is.close();
53     baos.close();
54     } catch (Exception e) {} //never mind if close throws an exception
55     }
56    
57     return baos.toByteArray();
58     }
59    
60 torben 634 public static Side9Data loadXml() throws IOException, ParserConfigurationException, SAXException{
61 torben 632 String dataURL = BASEURL + "xml.php?width=" + WIDTH;
62    
63     byte data[] = getContent( dataURL );
64 torben 649 String xmlData = new String(data, "ISO-8859-1");
65 torben 632 Document doc = parseXML(xmlData);
66    
67     Node rootNode = doc.getDocumentElement(); // stations
68     NodeList nodes = rootNode.getChildNodes();
69    
70     Side9Data side9Data = new Side9Data();
71    
72     for (int i=0; i<nodes.getLength(); i++) {
73     Node currentNode = nodes.item(i);
74    
75     String name = currentNode.getNodeName();
76     String content = null;
77    
78     if (currentNode.getFirstChild() != null)
79     content = currentNode.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
80    
81    
82     if (name.equals("imageurl"))
83     side9Data.url = BASEURL + content;
84     if (name.equals("width"))
85     side9Data.width = Integer.parseInt(content);
86     if (name.equals("height"))
87     side9Data.height = Integer.parseInt(content);
88     if (name.equals("caption"))
89     side9Data.caption = content;
90     }
91 torben 633
92     Log.i("Side9Xml", "url: " + side9Data.url);
93     Log.i("Side9Xml", "size: " + side9Data.width + "/" + side9Data.height);
94     Log.i("Side9Xml", "capt: " + side9Data.caption);
95    
96 torben 632
97    
98    
99    
100     return side9Data;
101     }
102    
103     }
104    

  ViewVC Help
Powered by ViewVC 1.1.20