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

  ViewVC Help
Powered by ViewVC 1.1.20