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

  ViewVC Help
Powered by ViewVC 1.1.20