인터넷 권한 있어야 합니다
manifest.xml -> permission -> INTERNET
<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webkit" android:layout_width="fill_parent" android:layout_height="fill_parent" />
package kr.android.weather; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import android.webkit.WebView; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; public class WeatherDemo extends Activity { //기상청 날씨정보 private static final String WEATHER_URL ="http://www.kma.go.kr/XML/weather/sfc_web_map.xml"; private WebView browser; private List<Forecast> forecasts = new ArrayList<Forecast>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); browser=(WebView)findViewById(R.id.webkit); updateForecast(); } private void updateForecast(){ buildForecastsByXmlPullParser(getStreamFromUrl()); String page=generatePage(); /* loadDataWithBaseURL ( String baseUrl:기본페이지, String data, String mimeType, String encoding, String historyUrl :에러시 돌아갈 페이지) */ browser.loadDataWithBaseURL(null,page,"text/html","UTF-8",null); } private InputStream getStreamFromUrl(){ InputStream input=null; try{ HttpClient client = new DefaultHttpClient(); HttpGet getMethod =new HttpGet(WEATHER_URL); //응답을 받을 객체 HttpResponse response =(HttpResponse)client.execute(getMethod); //응탑 수신 처리 HttpEntity entity =response.getEntity(); BufferedHttpEntity buf=new BufferedHttpEntity(entity); input =buf.getContent(); } catch (Exception e){ Log.e("WeatherDemo","Exception fetching data",e); Toast.makeText(this, "요청실패: "+e.toString(), 4000).show(); } return input; } public void buildForecastsByXmlPullParser(InputStream input){ String desc =null; String temp =null; String locale =null; try{ XmlPullParserFactory factory=XmlPullParserFactory.newInstance(); XmlPullParser parser = factory.newPullParser(); //파서에 스트림 연결 (default = "UTF-8") parser.setInput(input, null); while(parser.getEventType()!=XmlPullParser.END_DOCUMENT){ if(parser.getEventType()==XmlPullParser.START_TAG){ if(parser.getName().equals("local")){ //날씨처리 //3번째 속성 (0,1,2,3,4) desc =parser.getAttributeValue(2); //온도 처리 temp=parser.getAttributeValue(3); } }else if(parser.getEventType()==XmlPullParser.TEXT){ //지역정보 처리 locale =parser.getText(); }else if (parser.getEventType()==XmlPullParser.END_TAG){ forecasts.add(new Forecast(locale,desc,temp)); } //XML의 파서 커서를 다음 태그로 이동시킴 parser.next(); } }catch(Exception e){ e.printStackTrace(); } } public String generatePage() { //StringBuilder : StringBuffer 와 차이점은 StringBuilder는 동기화를 지원한다 StringBuilder bufResult=new StringBuilder("<html><body><table width=\"100%\">"); bufResult.append("<tr><th width=\"30%\">지역</th>"+ "<th width=\"50%\">날씨</th><th width=\"20%\">온도</th></tr>"); //확장 for문 for (Forecast forecast : forecasts) { bufResult.append("<tr><td align=\"center\">"); bufResult.append(forecast.locale); bufResult.append("</td><td align=\"center\">"); bufResult.append(forecast.desc); bufResult.append("</td><td>"); bufResult.append(forecast.temp); bufResult.append("</td></tr>"); } bufResult.append("</table></body></html>"); //StringBuilder를 String으로 넘겨줌 return(bufResult.toString()); } class Forecast { String locale; String desc; String temp; public Forecast(String locale,String desc,String temp){ this.locale = locale; this.desc = desc; this.temp = temp; } } }
'Android > 기본' 카테고리의 다른 글
안드로이드 서비스 동작중인 서비스 인지 체크 (0) | 2012.04.28 |
---|---|
안드로이드 프로세스 말끔히 죽이기 (0) | 2012.04.28 |
SQLite 연동하기 (1) | 2012.04.28 |
터치해서 화면 전환 Flipper Touch (0) | 2012.04.28 |
ListIcon (ListView에 아이콘,버튼) (0) | 2012.04.28 |