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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 793 - (show annotations) (download)
Thu Jun 3 09:39:56 2010 UTC (13 years, 11 months ago) by torben
File size: 2942 byte(s)
Download full-size image and downscale before handing over to widget
1 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 import android.util.Log;
20
21
22
23 public class Side9Xml {
24
25 static final int WIDTH = 450;
26 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 connection.setConnectTimeout(2500);
43 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 public static Side9Data loadXml() throws IOException, ParserConfigurationException, SAXException{
61 String dataURL = BASEURL + "xml.php?width=" + WIDTH;
62
63 byte data[] = getContent( dataURL );
64 String xmlData = new String(data, "ISO-8859-1");
65 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
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
97
98
99
100 return side9Data;
101 }
102
103 }
104

  ViewVC Help
Powered by ViewVC 1.1.20