weatherapi, retrofit2

This commit is contained in:
JUNGGWAN KIM 2024-09-20 13:55:04 +09:00
parent 5360a7ad13
commit 809b2b4279
3 changed files with 93 additions and 19 deletions

View File

@ -92,6 +92,9 @@ dependencies {
implementation("com.squareup.okhttp:okhttp:2.7.5") implementation("com.squareup.okhttp:okhttp:2.7.5")
implementation("com.google.android.gms:play-services-location:21.0.1") implementation("com.google.android.gms:play-services-location:21.0.1")
implementation("com.google.android.gms:play-services-tasks:18.2.0") implementation("com.google.android.gms:play-services-tasks:18.2.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.6.4")
implementation("com.squareup.retrofit2:converter-scalars:2.6.4")
// implementation ("androidx.window:window:1.0.0") // implementation ("androidx.window:window:1.0.0")
// implementation("io.github.vaneproject:hanguleditor:1.0.0") // implementation("io.github.vaneproject:hanguleditor:1.0.0")
} }

View File

@ -0,0 +1,38 @@
package rasel.lunar.launcher.model
import android.app.appsearch.StorageInfo
import com.google.gson.annotations.SerializedName
class WeatherInfo {
@SerializedName("coord")
private var coord: Location? = null
@SerializedName("weather")
private var weather: Weather? = null
@SerializedName("main")
private var main: Main? = null
}
class Location {
var lon: String? = null
var lat: String? = null
}
class Weather {
var id: String? = null
var main: String? = null
var description: String? = null
var icon: String? = null
}
class Main {
var temp: String? = null
var feels_like: String? = null
var temp_min: String? = null
var temp_max: String? = null
var pressure: String? = null
var humidity: String? = null
var sea_level: String? = null
var grnd_level: String? = null
}

View File

@ -7,7 +7,16 @@ import androidx.work.WorkerParameters
import com.google.android.gms.location.LocationServices import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource import com.google.android.gms.tasks.CancellationTokenSource
import rasel.lunar.launcher.model.WeatherInfo
import rasel.lunar.launcher.utils.BLog import rasel.lunar.launcher.utils.BLog
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import retrofit2.http.GET
import retrofit2.http.Path
class OpenWeatherGetter : BaseGetter { class OpenWeatherGetter : BaseGetter {
companion object { companion object {
@ -24,6 +33,7 @@ class OpenWeatherGetter : BaseGetter {
//87cd0810b7e4b4debd31a6ef98b98154 //87cd0810b7e4b4debd31a6ef98b98154
//{https://home.openweathermap.org/api 에서 정보를 조회 하자} //{https://home.openweathermap.org/api 에서 정보를 조회 하자}
getWeather()
// 정형화된 정보를 취드하여 realm db에 저장 하자 // 정형화된 정보를 취드하여 realm db에 저장 하자
@ -34,24 +44,47 @@ class OpenWeatherGetter : BaseGetter {
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
fun getLocation() { fun getLocation() {
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this.applicationContext) val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this.applicationContext)
// if (ActivityCompat.checkSelfPermission( fusedLocationProviderClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY,CancellationTokenSource().token)
// this.applicationContext, .addOnSuccessListener{ success: Location? ->
// Manifest.permission.ACCESS_FINE_LOCATION success?.let {
// ) == PackageManager.PERMISSION_GRANTED && BLog.LOGE("Location >>> $it")
// ActivityCompat.checkSelfPermission( BLog.LOGE("Location altitude >>> ${it.altitude}")
// this.applicationContext, BLog.LOGE("Location latitude >>> ${it.latitude}")
// Manifest.permission.ACCESS_COARSE_LOCATION
// ) == PackageManager.PERMISSION_GRANTED
// ) {
fusedLocationProviderClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY,CancellationTokenSource().token)
.addOnSuccessListener{ success: Location? ->
success?.let {
BLog.LOGE("Location >>> $it")
}
}.addOnFailureListener{
BLog.LOGE("Location error >>> $it")
} }
return }.addOnFailureListener{
// } BLog.LOGE("Location error >>> $it")
}
return
} }
}
fun getWeather() {
val retro = Retrofit.Builder()
.baseUrl("https://api.openweathermap.org")
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retro.create<RestrofitService>()
val call = service.getPosts("lat=44.34&lon=10.99&appid=87cd0810b7e4b4debd31a6ef98b98154")
call!!.enqueue(object: Callback<WeatherInfo>() {
override fun onResponse(
call: Call<WeatherInfo>,
response: Response<WeatherInfo>
) {
BLog.LOGE("Location >>> ")
}
override fun onFailure(call: Call<WeatherInfo>, t: Throwable) {
BLog.LOGE("Location >>> ")
}
})
}
}
interface RestrofitService {
@GET("/data/2.5/weather?{post}}")
fun getPosts(@Path("post") post: String?): Call<WeatherInfo?>?
}