public class MainActivity extends Activity {
Document doc = null;
ArrayList<WeatherVO> list; //현재지역 모든값 저장.
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetXMLTask task = new GetXMLTask();
task.execute("http://www.kma.go.kr/XML/weather/sfc_web_map.xml");
tv1 = (TextView) findViewById(R.id.tv1);
}
//private inner class extending AsyncTask
@SuppressLint("NewApi")
private class GetXMLTask extends AsyncTask<String, Void, Document>{
HashMap<String,HashMap<String,String>> resultList = new HashMap<String,HashMap<String,String>>();
String busan;
protected Document doInBackground(String... urls) {
URL url;
try {
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder builder =factory.newDocumentBuilder();
// Document doc = builder.parse("http://www.kma.go.kr/XML/weather/sfc_web_map.xml");
url = new URL(urls[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); //XML문서 빌더 객체를 생성
doc = db.parse(new InputSource(url.openStream())); //XML문서를 파싱한다.
doc.getDocumentElement().normalize();
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Parsing Error", Toast.LENGTH_SHORT).show();
}
return doc;
}
@Override
protected void onPostExecute(Document doc) {
NodeList local = doc.getElementsByTagName("local");
if(local.getLength() > 0){
String nodeValue = null;
HashMap<String,String> itemMap = null;
for(int i = 0; i < local.getLength();i++){
Element e = (Element) local.item(i);
nodeValue = e.getFirstChild().getNodeValue();
itemMap = new HashMap<String,String>();
itemMap.put("아이콘넘버", e.getAttribute("icon"));
itemMap.put("날씨", e.getAttribute("desc"));
itemMap.put("기온", e.getAttribute("ta"));
if(nodeValue != null&&itemMap !=null){
resultList.put(nodeValue, itemMap);
}
}//for end
busan = resultList.get("부산").get("날씨").toString();
} //if end
super.onPostExecute(doc);
/////////////////레이아웃 설정하는 구역
tv1.setText(busan);
}//onPostExecute end
}//GetXMLTask end
}//메인 end
일단 뻑나는데 머가 문제인지 몰르겠어요 ㅜㅜ
11-14 11:12:34.404: E/DataScheduler(32500): isDataSchedulerEnabled():false
11-14 11:12:34.404: W/dalvikvm(32500): threadid=11: thread exiting with uncaught exception (group=0x41839e48)
11-14 11:12:34.404: E/AndroidRuntime(32500): FATAL EXCEPTION: AsyncTask #1
11-14 11:12:34.404: E/AndroidRuntime(32500): Process: com.example.test, PID: 32500
11-14 11:12:34.404: E/AndroidRuntime(32500): java.lang.RuntimeException: An error occured while executing doInBackground()
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-14 11:12:34.404: E/AndroidRuntime(32500): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-14 11:12:34.404: E/AndroidRuntime(32500): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-14 11:12:34.404: E/AndroidRuntime(32500): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-14 11:12:34.404: E/AndroidRuntime(32500): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-14 11:12:34.404: E/AndroidRuntime(32500): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-14 11:12:34.404: E/AndroidRuntime(32500): at java.lang.Thread.run(Thread.java:841)
11-14 11:12:34.404: E/AndroidRuntime(32500): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.os.Handler.<init>(Handler.java:200)
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.os.Handler.<init>(Handler.java:114)
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.widget.Toast$TN.<init>(Toast.java:351)
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.widget.Toast.<init>(Toast.java:101)
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.widget.Toast.makeText(Toast.java:265)
11-14 11:12:34.404: E/AndroidRuntime(32500): at com.example.test.MainActivity$GetXMLTask.doInBackground(MainActivity.java:64)
11-14 11:12:34.404: E/AndroidRuntime(32500): at com.example.test.MainActivity$GetXMLTask.doInBackground(MainActivity.java:1)
11-14 11:12:34.404: E/AndroidRuntime(32500): at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-14 11:12:34.404: E/AndroidRuntime(32500): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-14 11:12:34.404: E/AndroidRuntime(32500): ... 4 more