11 |
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.parsers.DocumentBuilderFactory; |
12 |
import javax.xml.parsers.ParserConfigurationException; |
import javax.xml.parsers.ParserConfigurationException; |
13 |
|
|
14 |
|
|
15 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
16 |
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
17 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
18 |
import org.xml.sax.SAXException; |
import org.xml.sax.SAXException; |
19 |
|
|
20 |
|
|
21 |
|
import android.util.Log; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
public class Side9Xml { |
public class Side9Xml { |
26 |
|
|
27 |
static final int WIDTH = 100; |
static final int WIDTH = 450; |
28 |
static final String BASEURL = "http://apps.todic.net/side9/"; |
static final String BASEURL = "http://apps.todic.net/side9/"; |
29 |
|
|
30 |
public static Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException |
public static Document parseXML(byte data[]) throws SAXException, IOException, ParserConfigurationException |
31 |
{ |
{ |
32 |
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
33 |
return builder.parse( new ByteArrayInputStream(str.getBytes()) ); |
|
34 |
|
return builder.parse( new ByteArrayInputStream( data) ); |
35 |
} |
} |
36 |
|
|
37 |
|
|
38 |
public static byte[] getContent(String uri) throws IOException { |
public static byte[] getContent(String uri) throws IOException { |
39 |
byte buffer[] = new byte[256]; |
byte buffer[] = new byte[4096]; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
44 |
|
|
45 |
URL url = new URL(uri); |
URL url = new URL(uri); |
46 |
URLConnection connection = url.openConnection(); |
URLConnection connection = url.openConnection(); |
47 |
|
connection.setConnectTimeout(2500); |
48 |
InputStream is = connection.getInputStream(); |
InputStream is = connection.getInputStream(); |
49 |
|
|
50 |
try { |
try { |
62 |
return baos.toByteArray(); |
return baos.toByteArray(); |
63 |
} |
} |
64 |
|
|
65 |
public Side9Data loadXml() throws IOException, ParserConfigurationException, SAXException{ |
public static Side9Data loadXml(String androidID) throws IOException, ParserConfigurationException, SAXException{ |
66 |
String dataURL = BASEURL + "xml.php?width=" + WIDTH; |
String dataURL = BASEURL + "xml.php?width=" + WIDTH + "&androidID=" + androidID; |
67 |
|
Log.i("Side9Xml", "URL=" + dataURL); |
68 |
|
|
69 |
|
|
70 |
byte data[] = getContent( dataURL ); |
byte data[] = getContent( dataURL ); |
71 |
String xmlData = new String(data, "UTF-8"); |
Document doc = parseXML( data ); |
|
Document doc = parseXML(xmlData); |
|
72 |
|
|
73 |
Node rootNode = doc.getDocumentElement(); // stations |
Node rootNode = doc.getDocumentElement(); // stations |
74 |
NodeList nodes = rootNode.getChildNodes(); |
NodeList nodes = rootNode.getChildNodes(); |
94 |
if (name.equals("caption")) |
if (name.equals("caption")) |
95 |
side9Data.caption = content; |
side9Data.caption = content; |
96 |
} |
} |
97 |
|
|
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 |
|
|
103 |
|
|
104 |
|
|