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

  ViewVC Help
Powered by ViewVC 1.1.20