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 연결 #41

Open
wants to merge 1 commit into
base: youjin-park
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ google-services.json
.externalNativeBuild
.cxx
local.properties
/app/src/main/java/com/example/gdsc/weather/ApiKey.kt
22 changes: 16 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -11,19 +13,27 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GDSC"
tools:targetApi="31">
tools:targetApi="31"
android:usesCleartextTraffic="true">
<activity
android:name=".NavigationActivity"
android:name=".weather.WeatherActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".NavigationActivity"
android:exported="true"></activity>
<activity
android:name=".FoodActivity"
android:exported="true">
</activity>
android:exported="true"></activity>
<activity
android:name=".UiActivity"
android:exported="true">
</activity>
android:exported="true"></activity>
<activity
android:name=".member.MemberActivity"
android:exported="true">
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/com/example/gdsc/weather/WeatherActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.gdsc.weather

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.gdsc.databinding.ActivityWeatherBinding
import retrofit2.Call
import retrofit2.Response

class WeatherActivity : AppCompatActivity() {
private lateinit var weatherBinding: ActivityWeatherBinding
private val getWeatherService = WeatherServicePool.getWeather
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
weatherBinding = ActivityWeatherBinding.inflate(layoutInflater)
setContentView(weatherBinding.root)
getWeatherApi()
}
private fun getWeatherApi() {
getWeatherService.getWeather().enqueue(object : retrofit2.Callback<WeatherDto> {
override fun onResponse(
call: Call<WeatherDto>, response: Response<WeatherDto>
) {
if (response.isSuccessful) {
response.body()?.let {
weatherBinding.tvResult.text = response.body().toString()
Log.d("result", it.toString())
}
} else {
Log.d("error", "실패한 응답")
}
}

override fun onFailure(call: Call<WeatherDto>, t: Throwable) {
t.message?.let { Log.d("error", it) } ?: "서버통신 실패(응답값 X)"
}
})
}
}
23 changes: 23 additions & 0 deletions app/src/main/java/com/example/gdsc/weather/WeatherApiFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.gdsc.weather

import kotlinx.serialization.json.Json
import retrofit2.Retrofit
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import okhttp3.MediaType.Companion.toMediaType
object WeatherApiFactory {
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()
}
}

object WeatherServicePool {
val getWeather = WeatherApiFactory.retrofit.create(WeatherService::class.java)
}


36 changes: 36 additions & 0 deletions app/src/main/java/com/example/gdsc/weather/WeatherDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.gdsc.weather

import kotlinx.serialization.Serializable
@Serializable
data class WeatherDto( val response: Response )
@Serializable
data class Response(
val header: Header,
val body: Body
)
@Serializable
data class Header(
val resultCode : String,
val resultMsg : String
)
@Serializable
data class Body(
val dataType: String,
val items: Items,
val pageNo: Int,
val numOfRows: Int,
val totalCount: Int,
)
@Serializable
data class Items(
val item: List<Item>
)
@Serializable
data class Item(
val baseDate: String,
val baseTime: String,
val category: String,
val nx: Int,
val ny: Int,
val obsrValue: String
)
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/gdsc/weather/WeatherService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.gdsc.weather

import com.example.gdsc.weather.ApiKey.Companion.API_KEY
import retrofit2.http.GET
import retrofit2.http.Query
import retrofit2.Call

interface WeatherService {
@GET("getUltraSrtNcst?serviceKey=$API_KEY")
fun getWeather(
@Query("numOfRows") numOfRows: Int = 1, // 한 페이지 경과 수
@Query("pageNo") pageNo: Int = 1, // 페이지 번호
@Query("dataType") dataType: String = "JSON", // 응답 자료 형식
@Query("base_date") base_date: String = "20231103", // 발표 일자
@Query("base_time") base_time: String = "0600", // 발표 시각
@Query("nx") nx: Int = 55, // 예보지점 X 좌표
@Query("ny") ny: Int = 127, // 예보지점 Y 좌표
): Call<WeatherDto>
}
Binary file added app/src/main/res/drawable/img_dog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_weather.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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=".weather.WeatherActivity">

<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="200dp"
android:text="Result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>