protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
btnTest = (Button)findViewById(R.id.btnTest);
new
Thread() {
public
void
run() {
try
{
StringBuffer sBuffer =
new
StringBuffer();
URL url =
new
URL(urlAddr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if
(conn !=
null
){
conn.setConnectTimeout(
20000
);
conn.setUseCaches(
false
);
if
(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
InputStreamReader isr =
new
InputStreamReader(conn.getInputStream());
BufferedReader br =
new
BufferedReader(isr);
while
(
true
){
String line = br.readLine();
if
(line==
null
){
break
;
}
sBuffer.append(line);
}
br.close();
conn.disconnect();
}
}
xml = sBuffer.toString();
}
catch
(Exception e) {
Toast.makeText(getApplicationContext(),
"접근불가"
,Toast.LENGTH_LONG).show();
}
}
}.start();
btnTest.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
xml.replace(
"![CDATA["
,
null
).replace(
"]]"
,
null
);
parse();
}
});
}
public
void
parse(){
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
InputStream is =
new
ByteArrayInputStream(xml.getBytes());
Document doc = documentBuilder.parse(is);
Element element = doc.getDocumentElement();
NodeList items = element.getElementsByTagName(
"mntNm"
);
int
n = items.getLength();
StringBuffer sBuffer =
new
StringBuffer();
for
(
int
i=
0
; i < n ; i++){
Node item = items.item(i);
Node text = item.getFirstChild();
String itemValue = text.getNodeValue();
str = Integer.toString((
int
) Double.parseDouble(itemValue));
sBuffer.append(str);
}
tv.append(str);
}
catch
(Exception e) {
Toast.makeText(getApplicationContext(),
"에러"
,Toast.LENGTH_LONG).show();
}
}