Yahoo Weather API 를 이용하여 현재 위치의 날씨 정보를 조회하는 방법에 대해 알아보겠습니다.
개발 환경
설정
build.gradle -> dependencies 추가
1 implementation 'zh.wang.android:yweathergetter4a:1.3.0'
manifest -> permission 추가
1 2 3 4 5 6 7 <uses-permission android:name ="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name ="android.permission.INTERNET" /> <uses-permission android:name ="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name ="android.permission.ACCESS_COARSE_LOCATION" />
코드 작성
Activity 또는 Fragment에 YahooWeatherInfoListener
인터페이스를 implements 하면 gotWeatherInfo
함수를 오버라이딩(Override)을 하게 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 public class WeatherActivity extends Activity implements YahooWeatherInfoListener { @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_weather); } @Override public void gotWeatherInfo (WeatherInfo weatherInfo, YahooWeather.ErrorType errorType) { } }
이제 날씨 정보를 불러오기 위해 다음의 함수 중 상황에 맞게 호출합니다.
1 2 3 4 5 6 7 8 public void queryYahooWeatherByPlaceName (final Context context, final String cityAreaOrLocation, final YahooWeatherInfoListener result) public void queryYahooWeatherByLatLon (final Context context, final String lat, final String lon, final YahooWeatherInfoListener result) public void queryYahooWeatherByGPS (final Context context, final YahooWeatherInfoListener result)
이번 프로젝트에는 GPS를 사용하여 현재 위치의 날씨 정보를 얻었습니다. 쿼리 함수를 호출하면 오버라이딩한 gotWeatherInfo
함수를 통해 날씨 정보를 얻을 수 있습니다.
1 2 3 4 5 6 YahooWeather yahooWeather = YahooWeather.getInstance(); yahooWeather.setNeedDownloadIcons(true ); yahooWeather.setUnit(YahooWeather.UNIT.CELSIUS); yahooWeather.setSearchMode(YahooWeather.SEARCH_MODE.GPS); yahooWeather.queryYahooWeatherByGPS(getApplicationContext(), this );
날씨 정보를 한 번만 불러오는 것이 아니라 1분마다 얻기 위해 Timer를 사용하였습니다. Timer를 사용하기 위해 위의 코드를 함수로 만들었습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run () { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run () { searchByGPS(); } }); } }, 0 , 1000 * 60 ); public void searchByGPS () { yahooWeather.setNeedDownloadIcons(true ); yahooWeather.setUnit(YahooWeather.UNIT.CELSIUS); yahooWeather.setSearchMode(YahooWeather.SEARCH_MODE.GPS); yahooWeather.queryYahooWeatherByGPS(getApplicationContext(), this ); }
이제 1분 마다 날씨 정보를 불러올 수 있게 되었습니다. 현재 위치, 시간, 온도, 습도, 대기압, 풍향, 풍속 등의 다양한 날씨 정보를 화면에 표출하여 완성하였습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Override public void gotWeatherInfo (WeatherInfo weatherInfo, YahooWeather.ErrorType errorType) { if (weatherInfo != null ) { datetimeText.setText(dateFormat.format(new Date())); logitudeText.setText(weatherInfo.getAddress().getLongitude() + "" ); latitudeText.setText(weatherInfo.getAddress().getLatitude() + "" ); addressText.setText(weatherInfo.getAddress().getAddressLine(0 )); weatherText.setText(weatherInfo.getCurrentText()); temperatureText.setText(weatherInfo.getCurrentTemp() + " ºC" ); humidityText.setText(weatherInfo.getAtmosphereHumidity() + " %" ); pressureText.setText(weatherInfo.getAtmospherePressure()); windDirectionText.setText(weatherInfo.getWindDirection() + "˚" ); windSpeedText.setText(weatherInfo.getWindSpeed() + " m/s" ); windChillText.setText(weatherInfo.getWindChill() + " °F" ); visibilityText.setText(weatherInfo.getAtmosphereVisibility()); } }
전체 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 import android.os.Handler;import android.os.Looper;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import butterknife.BindView;import butterknife.ButterKnife;import me.hgko.networkinfo.R;import zh.wang.android.yweathergetter4a.WeatherInfo;import zh.wang.android.yweathergetter4a.YahooWeather;import zh.wang.android.yweathergetter4a.YahooWeatherInfoListener;public class WeatherActivity extends AppCompatActivity implements YahooWeatherInfoListener { private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss" ); @BindView (R.id.logitudeText) TextView logitudeText; @BindView (R.id.latitudeText) TextView latitudeText; @BindView (R.id.datetimeText) TextView datetimeText; @BindView (R.id.addressText) TextView addressText; @BindView (R.id.weatherText) TextView weatherText; @BindView (R.id.temperatureText) TextView temperatureText; @BindView (R.id.humidityText) TextView humidityText; @BindView (R.id.pressureText) TextView pressureText; @BindView (R.id.windDirectionText) TextView windDirectionText; @BindView (R.id.windSpeedText) TextView windSpeedText; @BindView (R.id.windChillText) TextView windChillText; @BindView (R.id.visibilityText) TextView visibilityText; private YahooWeather yahooWeather; private Timer timer; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_weather); ButterKnife.bind(this ); yahooWeather = YahooWeather.getInstance(); timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run () { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run () { searchByGPS(); } }); } }, 0 , 1000 * 60 * 10 ); } @Override public void gotWeatherInfo (WeatherInfo weatherInfo, YahooWeather.ErrorType errorType) { if (weatherInfo != null ) { datetimeText.setText(dateFormat.format(new Date())); logitudeText.setText(weatherInfo.getAddress().getLongitude() + "" ); latitudeText.setText(weatherInfo.getAddress().getLatitude() + "" ); addressText.setText(weatherInfo.getAddress().getAddressLine(0 )); weatherText.setText(weatherInfo.getCurrentText()); temperatureText.setText(weatherInfo.getCurrentTemp() + " ºC" ); humidityText.setText(weatherInfo.getAtmosphereHumidity() + " %" ); pressureText.setText(weatherInfo.getAtmospherePressure()); windDirectionText.setText(weatherInfo.getWindDirection() + "˚" ); windSpeedText.setText(weatherInfo.getWindSpeed() + " m/s" ); windChillText.setText(weatherInfo.getWindChill() + " °F" ); visibilityText.setText(weatherInfo.getAtmosphereVisibility()); } } private void searchByGPS () { yahooWeather.setNeedDownloadIcons(true ); yahooWeather.setUnit(YahooWeather.UNIT.CELSIUS); yahooWeather.setSearchMode(YahooWeather.SEARCH_MODE.GPS); yahooWeather.queryYahooWeatherByGPS(getApplicationContext(), this ); } @Override public void onDestroy () { super .onDestroy(); if (timer != null ) { timer.cancel(); timer = null ; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:tools ="http://schemas.android.com/tools" android:layout_width ="match_parent" android:layout_height ="match_parent" android:paddingBottom ="20dp" android:paddingLeft ="20dp" android:paddingRight ="20dp" android:paddingTop ="20dp" tools:context =".activity.WeatherActivity" > <ScrollView android:layout_width ="match_parent" android:layout_height ="match_parent" > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:orientation ="vertical" > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Datetime :" /> <TextView android:id ="@+id/datetimeText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Logitude :" /> <TextView android:id ="@+id/logitudeText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Latitude :" /> <TextView android:id ="@+id/latitudeText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Address :" /> <TextView android:id ="@+id/addressText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Weather :" /> <TextView android:id ="@+id/weatherText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Temperature :" /> <TextView android:id ="@+id/temperatureText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Wind Chill :" /> <TextView android:id ="@+id/windChillText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Wind Direction :" /> <TextView android:id ="@+id/windDirectionText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Wind Speed :" /> <TextView android:id ="@+id/windSpeedText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Humidity :" /> <TextView android:id ="@+id/humidityText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Pressure :" /> <TextView android:id ="@+id/pressureText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > <LinearLayout android:layout_width ="match_parent" android:layout_height ="wrap_content" android:layout_marginTop ="8dp" android:orientation ="horizontal" > <TextView style ="@style/TextStyle1" android:text ="Visibility :" /> <TextView android:id ="@+id/visibilityText" style ="@style/TextStyle2" android:textColor ="@color/colorBlue" /> </LinearLayout > </LinearLayout > </ScrollView > </LinearLayout >
TextView 에 공통으로 스타일을 지정하기 위해 styles.xml 에 추가합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <style name ="TextStyle1" > <item name ="android:layout_width" > 0dp</item > <item name ="android:layout_height" > wrap_content</item > <item name ="android:layout_weight" > 1</item > <item name ="android:textColor" > @color/colorText</item > <item name ="android:textSize" > 14sp</item > </style > <style name ="TextStyle2" > <item name ="android:layout_width" > 0dp</item > <item name ="android:layout_height" > wrap_content</item > <item name ="android:layout_weight" > 2</item > <item name ="android:textColor" > @color/colorText</item > <item name ="android:textSize" > 14sp</item > </style >
실행 결과
앱을 실행하면 아래 이미지와 같이 현재 위치의 날씨 정보를 확인할 수 있습니다.
참고