Android HttpClient, Google Weather API, XmlPullParser example
안드로이드에서 HttpClient, XmlPullParser, Google Weather API를 이용하여 현재날씨와 날씨 아이콘을 출력하는 예
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="서울의 오늘 날씨" /> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="30dip" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="온도 : " /> <TextView android:id="@+id/tvTemp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3" android:text="TextView" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="30dip" > <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="습도 : " /> <TextView android:id="@+id/tvHumidity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3" android:text="TextView" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="30dip" > <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="바람 :" /> <TextView android:id="@+id/tvWind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3" android:text="TextView" /> </LinearLayout> </LinearLayout>
MainActivity.java
package com.example.androidapp; import java.io.*; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import android.app.*; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.*; import android.util.*; import android.widget.*; public class MainActivity extends Activity { ImageView iv; TextView tvTemp, tvHumidity, tvWind; Handler handler = new Handler(); Bitmap icon; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv = (ImageView) findViewById(R.id.imageView1); tvTemp = (TextView) findViewById(R.id.tvTemp); tvHumidity = (TextView) findViewById(R.id.tvHumidity); tvWind = (TextView) findViewById(R.id.tvWind); new Thread() { public void run() { connect(); } }.start(); } String temp_c, humidity, wind_condition, icon_path; private void connect() { try{ HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://www.google.co.kr/ig/api?weather=seoul,korea"); HttpResponse response = httpClient.execute(httpGet); InputStream is = response.getEntity().getContent(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser parser = factory.newPullParser(); parser.setInput(is, "euc-kr"); int eventType = parser.getEventType(); String tagName = null; while(eventType != XmlPullParser.END_DOCUMENT) { if(eventType == XmlPullParser.START_TAG) { tagName = parser.getName(); if(tagName.equals("current_conditions")) { parser.next(); parser.next(); parser.next(); parser.next(); parser.next(); tagName = parser.getName(); //Log.e("태그이름", tagName); if(tagName.equals("temp_c")) { temp_c = parser.getAttributeValue(0); Log.e("온도", temp_c); parser.next(); parser.next(); humidity = parser.getAttributeValue(0); Log.e("습도", humidity); parser.next(); parser.next(); icon_path = parser.getAttributeValue(0); icon = getIcon(icon_path); Log.e("아이콘", icon_path); parser.next(); parser.next(); wind_condition = parser.getAttributeValue(0); Log.e("바람", wind_condition); handler.post(new Runnable() { public void run() { iv.setImageBitmap(icon); tvTemp.setText(temp_c); tvHumidity.setText(humidity); tvWind.setText(wind_condition); } }); break; } } } eventType = parser.next(); } // end of while() }catch(Exception ex) { ex.printStackTrace(); Log.e("접속오류", ex.toString()); } } private Bitmap getIcon(String icon_path) { // 아이콘 경로에서 아이콘을 가져와 리턴한다 try{ HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://www.google.co.kr"+icon_path); HttpResponse response = httpClient.execute(httpGet); InputStream is = response.getEntity().getContent(); return BitmapFactory.decodeStream(is); }catch(Exception ex) { ex.printStackTrace(); } return null; } }