diff --git a/app/.idea/gradle.xml b/app/.idea/gradle.xml new file mode 100644 index 0000000..de8896e --- /dev/null +++ b/app/.idea/gradle.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9adcaa2..cf6ea37 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,9 +14,12 @@ android:supportsRtl="true" android:theme="@style/Theme.GDSC" tools:targetApi="31"> + + android:exported="true"> @@ -25,9 +28,7 @@ - - + android:exported="true"> \ No newline at end of file diff --git a/app/src/main/java/com/example/gdsc/week5/ApiFactory.kt b/app/src/main/java/com/example/gdsc/week5/ApiFactory.kt new file mode 100644 index 0000000..21bc2ff --- /dev/null +++ b/app/src/main/java/com/example/gdsc/week5/ApiFactory.kt @@ -0,0 +1,18 @@ +package com.example.gdsc.week5 + +import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory +import kotlinx.serialization.json.Json +import okhttp3.MediaType.Companion.toMediaType +import retrofit2.Retrofit + +object ApiFactory { + private const val BASE_URL = + "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0" + + val retrofit: Retrofit by lazy { + Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) + .build() + } +} diff --git a/app/src/main/java/com/example/gdsc/week5/ServicePool.kt b/app/src/main/java/com/example/gdsc/week5/ServicePool.kt new file mode 100644 index 0000000..0056cc3 --- /dev/null +++ b/app/src/main/java/com/example/gdsc/week5/ServicePool.kt @@ -0,0 +1,6 @@ +package com.example.gdsc.week5 + + +object ServicePool { + val getTodayWeather = ApiFactory.retrofit.create(WeatherApiService::class.java) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/gdsc/week5/WeatherActivity.kt b/app/src/main/java/com/example/gdsc/week5/WeatherActivity.kt new file mode 100644 index 0000000..f35cb03 --- /dev/null +++ b/app/src/main/java/com/example/gdsc/week5/WeatherActivity.kt @@ -0,0 +1,56 @@ +package com.example.gdsc.week5 + +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.util.Log +import com.example.gdsc.R +import retrofit2.Call +import retrofit2.Response +import java.time.LocalDate +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter + +class WeatherActivity : AppCompatActivity() { + + private val getWeatherService = ServicePool.getTodayWeather + + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_weather) + getWeatherrApi() + } + + private fun getWeatherrApi() { + val todayDate: LocalDate = LocalDate.now() + val formatter = DateTimeFormatter.ofPattern("yyyyMMdd") + val formatted_td = todayDate.format(formatter) + + getWeatherService.getTodayWeather( + "xchJvCufUN4CvhDfeaLEU1rLlup%2B4jNH9RSALuQi97KuXpkVWZtPjHK7KUbEoHhWZ5gTmz2xOdtdgbFrpfP7Qg", + 1, + 1, + "JSON", + formatted_td.toString(), + "0600", + 55, + 127, + ).enqueue(object : retrofit2.Callback { + override fun onResponse( + call: Call, response: Response + ) { + if (response.isSuccessful) { + response.body()?.let { + Log.d("api_result", it.toString()) + } + } else { + Log.d("error", "실패한 응답") + } + } + + override fun onFailure(call: Call, t: Throwable) { + t.message?.let { Log.d("error", it) } ?: "서버통신 실패(응답값 X)" + } + }) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/gdsc/week5/WeatherApiService.kt b/app/src/main/java/com/example/gdsc/week5/WeatherApiService.kt new file mode 100644 index 0000000..7179ec6 --- /dev/null +++ b/app/src/main/java/com/example/gdsc/week5/WeatherApiService.kt @@ -0,0 +1,19 @@ +package com.example.gdsc.week5 + +import retrofit2.Call +import retrofit2.http.GET +import retrofit2.http.Query + +interface WeatherApiService { + @GET("/getUltraSrtNcst") + fun getTodayWeather( + @Query("serviceKey") serviceKey: String, + @Query("numOfRows") numOfRows: Int = 1, + @Query("pageNo") pageNo: Int = 1, + @Query("dataType") dataType: String, + @Query("base_date") base_date: String, + @Query("base_time") base_time: String, + @Query("nx") nx: Int, + @Query("ny") ny: Int + ): Call +} \ No newline at end of file diff --git a/app/src/main/java/com/example/gdsc/week5/WeatherDto.kt b/app/src/main/java/com/example/gdsc/week5/WeatherDto.kt new file mode 100644 index 0000000..2f4d1cb --- /dev/null +++ b/app/src/main/java/com/example/gdsc/week5/WeatherDto.kt @@ -0,0 +1,65 @@ +package com.example.gdsc.week5 + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class WeatherDto( + @SerialName("response") + val response: Response +){ + @Serializable + data class Response( + @SerialName("header") + val header: Header, + @SerialName("body") + val body: Body + ){ + @Serializable + data class Header( + @SerialName("resultCode") + val resultCode: String, + @SerialName("resultMsg") + val resultMsg: String + ) + @Serializable + data class Body( + @SerialName("dataType") + val dataType: String, + @SerialName("items") + val items: Items, + @SerialName("numOfRows") + val numOfRows: Int, + @SerialName("pageNo") + val pageNo: Int, + @SerialName("totalCount") + val totalCount: Int + ){ + @Serializable + data class Items( + @SerialName("item") + val item: List + ){ + @Serializable + data class Item( + @SerialName("baseDate") + val baseDate: String, + @SerialName("baseTime") + val baseTime: String, + @SerialName("category") + val category: String, + @SerialName("nx") + val nx: Int, + @SerialName("ny") + val ny: Int, + @SerialName("obsrValue") + val obsrValue: String + ) + } + } + + } +} + + + diff --git a/app/src/main/res/layout/activity_weather.xml b/app/src/main/res/layout/activity_weather.xml new file mode 100644 index 0000000..7fef4cc --- /dev/null +++ b/app/src/main/res/layout/activity_weather.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file