안드로이드 개발 질문/답변
(글 수 45,052)
GeoToWeather
public class GeoToWeather {
private Geocoder geoCoder;
private GeoPoint geoPoint;
public GeoToWeather(Geocoder t_gc, GeoPoint t_gp)
{
geoCoder = t_gc;
geoPoint = t_gp;
}
public Weather getWeather()
{
List<Address> tList=null;
try {
tList = geoCoder.getFromLocation((double)geoPoint.getLatitudeE6()/1000000,
(double)geoPoint.getLongitudeE6()/1000000, 5);
} catch (IOException e) {
}
Address tAddr = tList.get(0);
Weather dataWeather = new Weather();
dataWeather.m_sRegion = tAddr.getLocality();
XmlPullParserFactory factory = null;
try{
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = null;
xpp = factory.newPullParser();
String connectUrl = "http://www.google.co.kr/ig/api?weather="
+ dataWeather.m_sRegion;
URL UrlRecWeather = null;
UrlRecWeather = new URL("connectUrl);
InputStream in;
in = UrlRecWeather.openStream();
xpp.setInput(in, "euc-kr");
ReceiveParsing getParse = new ReceiveParsing(dataWeather);
getParse.proceed(xpp);
}
catch(Exception ex)
{
}
return dataWeather;
}
public void chageGeoPoint(GeoPoint t_gp)
{
geoPoint = t_gp;
}
class ReceiveParsing {
Weather dataWeather;
public ReceiveParsing(Weather t_dW)
{
dataWeather = t_dW;
}
void proceed(XmlPullParser ReceiveStream) {
boolean bcurrent_condition = false;
try {
String sTag;
int eventType = ReceiveStream.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.END_DOCUMENT:
break;
case XmlPullParser.START_TAG:
// items.add(xpp.getAttributeValue(0));
sTag = ReceiveStream.getName();
if (sTag.equals("current_conditions")) {
bcurrent_condition = true;
}
if (bcurrent_condition == true) {
if (sTag.equals("condition")) {
String sValue = ReceiveStream.getAttributeValue(0);
dataWeather.m_sCurrentState = sValue;
} else if (sTag.equals("temp_f")) {
String sValue = ReceiveStream.getAttributeValue(0);
dataWeather.m_nTempF = Integer.parseInt(sValue);
} else if (sTag.equals("temp_c")) {
String sValue = ReceiveStream.getAttributeValue(0);
dataWeather.m_nTempC = Integer.parseInt(sValue);
} else if (sTag.equals("humidity")) {
String sValue = ReceiveStream.getAttributeValue(0);
dataWeather.m_sHumidity = sValue;
} else if (sTag.equals("wind_condition")) {
String sValue = ReceiveStream.getAttributeValue(0);
dataWeather.m_sWindCondition = sValue;
}
}
break;
case XmlPullParser.END_TAG:
sTag = ReceiveStream.getName();
if (sTag.equals("current_conditions")) {
bcurrent_condition = false;
}
break;
case XmlPullParser.TEXT:
break;
}
eventType = ReceiveStream.next();
}
} catch (Exception e) {}
-------------------------------------------------------------------------------------------------------------------------
WeatherActivity.java
public class WeatherActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Geocoder t_Geocoder = new Geocoder(this);
GeoPoint t_GeoPoint = new GeoPoint(37517292, 127037187);
GeoToWeather gtw = new GeoToWeather(t_Geocoder, t_GeoPoint);
Weather t_Weather = gtw.getWeather();
...
TextView t_textview = new TextView(this);
t_textview.setText(tStr);
t_textview.setTextSize(30);
setContentView(t_textview);
}
}
-------------------------------------------------------------------------------------------------------------------------
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
-------------------------------------------------------------------------------------------------------------------------
GeoPoint로 날씨정보를 가져오는 소스입니다.
소스부분에서 딱히 틀린게 없어 보이는데 실행하면 에뮬이나 단말기에서 에러가 납니다.
에러부분 로그캣을 첨부하였습니다.
틀린부분좀 알려주세요ㅠ (앞전에 풀소스를 올려서 죄송합니다. 지금도 소스가 좀 많긴 하네요ㅠ)