diff --git a/WidgetTest01/.idea/misc.xml b/WidgetTest01/.idea/misc.xml index 3963879..75dac50 100644 --- a/WidgetTest01/.idea/misc.xml +++ b/WidgetTest01/.idea/misc.xml @@ -24,7 +24,7 @@ - + diff --git a/WidgetTest01/app/src/main/java/org/techtown/widgettest01/MainActivity.java b/WidgetTest01/app/src/main/java/org/techtown/widgettest01/MainActivity.java index f323bbd..3fe6029 100644 --- a/WidgetTest01/app/src/main/java/org/techtown/widgettest01/MainActivity.java +++ b/WidgetTest01/app/src/main/java/org/techtown/widgettest01/MainActivity.java @@ -25,12 +25,14 @@ import javax.net.ssl.HttpsURLConnection; public class MainActivity extends AppCompatActivity { - TextView textView; + TextView location; TextView tc; TextView minNmax; + TextView currentIndex; + TextView UV; FloatingActionButton fab_add, fab_mypage, fab_main; - String[] items = {"강남구", "강동구", "강북구", "강서구", "관악구", "광진구", "구로구", "금천구", "노원구", "도봉구", "동대문구", "동작구", "마포구", "서대문구", "서초구", "성동구", "성북구", "송파구", "양천구", "영등포구", "용산구", "은평구", "종로구", "중구", "중랑구"}; - String[] villages = {"삼성동", "성내1동", "수유3동", "화곡6동", "봉천동", "자양동", "구로동", "시흥동", "상계6.7동", "방학동", "용두동", "노량진2동", "성산동", "연희동", "서초2동", "행당동", "삼선동", "신천동", "신정6동", "당산동", "이태원동", "녹번동", "수송동", "광희동", "신내동"}; + String[] items = {"강남구","강동구","강북구","강서구","관악구","광진구","구로구","금천구","노원구","도봉구","동대문구","동작구","마포구","서대문구","서초구","성동구","성북구","송파구","양천구","영등포구","용산구","은평구","종로구","중구","중랑구"}; + String[] villages = {"삼성동","성내1동","수유3동","화곡6동","봉천동","자양동","구로동","시흥동","상계6.7동","방학동","용두동","노량진2동","성산동","연희동","서초2동","행당동","삼선동","신천동","신정6동","당산동","이태원동","녹번동","수송동","광희동","신내동"}; int countyPosition; @Override @@ -38,9 +40,11 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - textView = (TextView) findViewById(R.id.textView); + location = (TextView) findViewById(R.id.location); minNmax = (TextView) findViewById(R.id.minNmax); tc = (TextView) findViewById(R.id.tc); + currentIndex = (TextView) findViewById(R.id.currentIndex); + UV = (TextView) findViewById(R.id.UV); fab_add = (FloatingActionButton)findViewById(R.id.fab_add); fab_mypage = (FloatingActionButton)findViewById(R.id.fab_mypage); @@ -57,7 +61,7 @@ protected void onCreate(Bundle savedInstanceState) { // 아이템 선택시 호출됨 @Override public void onItemSelected(AdapterView adapterView, View view, int position, long id) { - textView.setText(items[position] + " " +villages[position]); + location.setText(items[position] + " " +villages[position]); countyPosition = position; String baseUrl = "https://api2.sktelecom.com/weather/current/minutely?appkey=8ab05bc6-8297-4251-bb89-e7a085516286&version=1"; @@ -77,6 +81,8 @@ public void onItemSelected(AdapterView adapterView, View view, int position, longitude = tInfo[3]; latitude = tInfo[4]; + Log.d("longitude 가져옴?", longitude); + Log.d("latitude 가져옴?", latitude); } catch (InterruptedException e) { Log.d("weatherAPI 결과1", "에러1"); e.printStackTrace(); @@ -85,11 +91,26 @@ public void onItemSelected(AdapterView adapterView, View view, int position, e.printStackTrace(); } - baseUrl = ""; - url = baseUrl + longitude + latitude; + baseUrl = "https://api2.sktelecom.com/weather/index/wct?appkey=8ab05bc6-8297-4251-bb89-e7a085516286&version=1"; + String lat = "&lat=" + latitude; + String lon = "&lon=" + longitude; + url = baseUrl + lat + lon; indexAPI iAPI = new indexAPI(url, longitude, latitude); try { String index = iAPI.execute().get(); + currentIndex.setText(index); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + baseUrl = "https://api2.sktelecom.com/weather/index/uv?appkey=8ab05bc6-8297-4251-bb89-e7a085516286&version=1"; + url = baseUrl + lat + lon; + UVAPI uvAPI = new UVAPI(url, longitude, latitude); + try{ + String comment = uvAPI.execute().get(); + UV.setText(comment); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { @@ -100,7 +121,7 @@ public void onItemSelected(AdapterView adapterView, View view, int position, // 아무것도 선택되지 않았을때 호출됨 -> 여기에 기본 설정 지역을.. @Override public void onNothingSelected(AdapterView adapterView) { - textView.setText(""); + location.setText(""); } }); diff --git a/WidgetTest01/app/src/main/java/org/techtown/widgettest01/UVAPI.java b/WidgetTest01/app/src/main/java/org/techtown/widgettest01/UVAPI.java new file mode 100644 index 0000000..0bf6b9a --- /dev/null +++ b/WidgetTest01/app/src/main/java/org/techtown/widgettest01/UVAPI.java @@ -0,0 +1,84 @@ +package org.techtown.widgettest01; + +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; + +/** + * Created by 수연 on 2018-08-14. + */ + +public class UVAPI extends AsyncTask, Integer, String> { + String urlStr; + String line; + String longitude; + String latitude; + + public UVAPI(String urlStr, String longitude, String latitude) { + this.urlStr = urlStr; + this.longitude = longitude; + this.latitude = latitude; + } + + @Override + protected String doInBackground(Map[] maps) { + Log.d("urlString", urlStr); + try { + URL url = new URL(urlStr); + HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { + InputStream in = urlConnection.getInputStream(); + + StringBuilder builder = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); + while ((line = reader.readLine()) != null) { + builder.append(line + "\n"); + } + + String result = builder.toString(); + String comment = ""; + + try { + JSONObject jsonObject = new JSONObject(result); + JSONObject weather = jsonObject.getJSONObject("weather"); + JSONObject wIndex = weather.getJSONObject("wIndex"); + JSONArray wctIndex = wIndex.getJSONArray("uvindex"); + JSONObject c = wctIndex.getJSONObject(0); + + JSONObject currentJSON = c.getJSONObject("day00"); + + comment = currentJSON.getString("comment"); + + Log.d(">>>>> comment", comment); + } catch (JSONException e) { + Log.d("제이슨 만들기", "실패"); + e.printStackTrace(); + } + + urlConnection.disconnect(); + return comment; + } else { + Log.d("UV-API연결", "실패"); + } + } catch (MalformedURLException e) { + Log.d("에러1", "MalformedURL"); + e.printStackTrace(); + } catch (IOException e) { + Log.d("에러2", "IOException"); + e.printStackTrace(); + } + return null; + } +} diff --git a/WidgetTest01/app/src/main/java/org/techtown/widgettest01/indexAPI.java b/WidgetTest01/app/src/main/java/org/techtown/widgettest01/indexAPI.java index 2ed7982..f939b44 100644 --- a/WidgetTest01/app/src/main/java/org/techtown/widgettest01/indexAPI.java +++ b/WidgetTest01/app/src/main/java/org/techtown/widgettest01/indexAPI.java @@ -3,6 +3,10 @@ import android.os.AsyncTask; import android.util.Log; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -42,8 +46,31 @@ protected String doInBackground(Map[] maps) { while ((line = reader.readLine()) != null) { builder.append(line + "\n"); } + + String result = builder.toString(); + String index = ""; + + try { + JSONObject jsonObject = new JSONObject(result); + JSONObject weather = jsonObject.getJSONObject("weather"); + JSONObject wIndex = weather.getJSONObject("wIndex"); + JSONArray wctIndex = wIndex.getJSONArray("wctIndex"); + JSONObject c = wctIndex.getJSONObject(0); + + JSONObject currentJSON = c.getJSONObject("current"); + + index = currentJSON.getString("index"); + + Log.d(">>>>> index", index); + } catch (JSONException e) { + Log.d("제이슨 만들기", "실패"); + e.printStackTrace(); + } + + urlConnection.disconnect(); + return index; } else { - Log.d("API연결", "실패"); + Log.d("index-API연결", "실패"); } } catch (MalformedURLException e) { Log.d("에러1", "MalformedURL"); diff --git a/WidgetTest01/app/src/main/res/layout/activity_main.xml b/WidgetTest01/app/src/main/res/layout/activity_main.xml index 995f570..6a5ff13 100644 --- a/WidgetTest01/app/src/main/res/layout/activity_main.xml +++ b/WidgetTest01/app/src/main/res/layout/activity_main.xml @@ -41,7 +41,7 @@ + + + + diff --git a/WidgetTest01/app/src/main/res/layout/activity_popup_clothes.xml b/WidgetTest01/app/src/main/res/layout/activity_popup_clothes.xml index 81c3985..43440f1 100644 --- a/WidgetTest01/app/src/main/res/layout/activity_popup_clothes.xml +++ b/WidgetTest01/app/src/main/res/layout/activity_popup_clothes.xml @@ -5,6 +5,7 @@ android:layout_width="300dp" android:layout_height="wrap_content" android:orientation="vertical" + android:id="@+id/popupButtons" tools:context="org.techtown.widgettest01.popup_clothes">