Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEEK5]: 공공 API 연결 #45

Open
wants to merge 1 commit into
base: yerim-lee
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
android:supportsRtl="true"
android:theme="@style/Theme.GDSC"
tools:targetApi="31">
<activity
android:name=".week5.WeatherActivity"
android:exported="false" />
<activity
android:name=".week4.MemberListActivity"
android:exported="true" >
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -25,9 +28,7 @@
</activity>
<activity
android:name=".week3.MainActivity"
android:exported="true">

</activity>
android:exported="true"></activity>
</application>

</manifest>
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/gdsc/week5/ApiFactory.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/example/gdsc/week5/ServicePool.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.gdsc.week5


object ServicePool {
val getTodayWeather = ApiFactory.retrofit.create(WeatherApiService::class.java)
}
56 changes: 56 additions & 0 deletions app/src/main/java/com/example/gdsc/week5/WeatherActivity.kt
Original file line number Diff line number Diff line change
@@ -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<WeatherDto> {
override fun onResponse(
call: Call<WeatherDto>, response: Response<WeatherDto>
) {
if (response.isSuccessful) {
response.body()?.let {
Log.d("api_result", it.toString())
}
} else {
Log.d("error", "실패한 응답")
}
}

override fun onFailure(call: Call<WeatherDto>, t: Throwable) {
t.message?.let { Log.d("error", it) } ?: "서버통신 실패(응답값 X)"
}
})
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/gdsc/week5/WeatherApiService.kt
Original file line number Diff line number Diff line change
@@ -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<WeatherDto>
}
65 changes: 65 additions & 0 deletions app/src/main/java/com/example/gdsc/week5/WeatherDto.kt
Original file line number Diff line number Diff line change
@@ -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<Item>
){
@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
)
}
}

}
}



9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_weather.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".week5.WeatherActivity">

</androidx.constraintlayout.widget.ConstraintLayout>