Weather App JetPack Compose #7 : getData()

build.gradle (module dependencies)

dependencies {
implementation "com.android.volley:volley:1.2.1"
implementation "com.google.accompanist:accompanist-pager:0.19.0"
implementation "com.google.accompanist:accompanist-pager-indicators:0.19.0"

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation("io.coil-kt:coil-compose:2.0.0-rc01")
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}

MainActivity

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import com.android.volley.Request
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.meter_alc_rgb.weatherappcomposey.screens.MainCard
import com.meter_alc_rgb.weatherappcomposey.screens.TabLayout
import com.meter_alc_rgb.weatherappcomposey.ui.theme.WeatherAppComposeYTheme

const val API_KEY = "Ваш API KEY"
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
WeatherAppComposeYTheme {
getData("London", this)
Image(
painter = painterResource(
id = R.drawable.weather_bg
),
contentDescription = "im1",
modifier = Modifier
.fillMaxSize()
.alpha(0.5f),
contentScale = ContentScale.FillBounds
)
Column{
MainCard()
TabLayout()
}

}
}
}
}

private fun getData(city: String, context: Context){
val url = "https://api.weatherapi.com/v1/forecast.json?key=$API_KEY" +
"&q=$city" +
"&days=" +
"3" +
"&aqi=no&alerts=no"
val queue = Volley.newRequestQueue(context)
val sRequest = StringRequest(
Request.Method.GET,
url,
{
response ->
Log.d("MyLog", "Response: $response")
},
{
Log.d("MyLog", "VolleyError: $it")
}
)
queue.add(sRequest)
}

8 комментариев для “Weather App JetPack Compose #7 : getData()”

  1. Добрый день! Постоянно получаю ошибку
    Error com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host “api.weatherapi.com”: No address associated with hostname

    Не могу понять в чем дело, подскажите пожалуйста, как это исправить

  2. Функция getdata() отрабатывает два раза, в logcat два debug-сообщения. Как заставить функцию запускаться один раз, а не при каждом событии отрисовки? Визуально задержка небольшая, но происходит увеличение нагрузки на сервер, и при тарифицированном запросе увеличивается его стоимость

Добавить комментарий для Ринат Отменить ответ

Ваш адрес email не будет опубликован.