Данные с сервера не грузятся , вместо них показывает null
Не знаю в чем проблема , при запуске приложения ,показывает null,видимо данные с сервера не пришли, использую retrofit,помогите пожалуйста ,заранее спасибо)
Main Activity
`class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MainViewModel
private lateinit var GET:SharedPreferences
private lateinit var SET:SharedPreferences.Editor
private lateinit var binding:ActivityMainBinding
val API: String = "34ae534ae4ff9829f963590e080ea518"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding =ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
//setContentView(R.layout.activity_main)
GET=getSharedPreferences(packageName, MODE_PRIVATE)
SET=GET.edit()
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
var cName =GET.getString("cityName","ankara")
binding.edtCityName.setText(cName)
viewModel.refreshData(cName!!)
getLiveData()
binding.swipeRefreshLayout.setOnRefreshListener {
binding.llDataView.visibility=View.GONE
binding.tvError.visibility=View.GONE
binding.pbLoading.visibility=View.GONE
var cityName = GET.getString("cityName",cName)
binding.edtCityName.setText(cityName)
viewModel.refreshData(cityName!!)
binding.swipeRefreshLayout.isRefreshing=false
}
binding.imgSearchCityName.setOnClickListener{
val cityName =binding.edtCityName.text.toString()
SET.putString("cityName",cityName)
SET.apply()
viewModel.refreshData(cityName!!)
getLiveData()
}
}
private fun getLiveData() {
viewModel.weather_data.observe(this, Observer { data ->
data.let {
binding.llDataView.visibility= View.VISIBLE
binding.pbLoading.visibility=View.GONE
binding.tvDegree.text = data?.main?.temp.toString()
binding.tvCountryCode.text = data?.sys?.country.toString()
binding.tvCityName.text = data?.name.toString()
binding.tvHumidity.text = data?.main?.humidity.toString()
binding.tvSpeed.text = data?.wind?.speed.toString()
binding.tvLat.text =data?.coord?.lat.toString()
binding.tvLon.text = data?.coord?.lon.toString()
Glide.with(this).load("http://openweathermap.org/img/wn/"+data?.weather?.get(0)?.icon+"[email protected]")
.into(binding.imgWeatherIcon)
}
})
viewModel.weather_load.observe(this, Observer { load->
load.let {
if(it == true){
binding.pbLoading.visibility=View.VISIBLE
binding.tvError.visibility=View.GONE
binding.llDataView.visibility=View.GONE
}else{
binding.pbLoading.visibility=View.GONE
}
}
})
viewModel.weather_error.observe(this, Observer { error->
error?.let {
if(it){
binding.tvError.visibility = View.VISIBLE
binding.llDataView.visibility = View.GONE
binding.pbLoading.visibility = View.GONE
}else{
binding.tvError.visibility =View.GONE
}
}
})
}
}
`
* Dependencies*
`implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//for refresh
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
//life cycle
//retrofit
implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
//rxjava
implementation"io.reactivex.rxjava2:rxjava:2.1.1"
implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
//pictures
implementation "com.github.bumptech.glide:glide:4.12.0"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3"`
* WeatherARI*
` package com.example.weather30.service
import com.example.weather30.model.WeatherModel
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Query
//https://api.openweathermap.org/data/2.5/weather?
q=Omsk&units=metric&appid=34ae534ae4ff9829f963590e080ea518
interface WeatherAPI {
@GET("data/2.5/weather?&units=metric&appid=34ae534ae4ff9829f963590e080ea518")
fun getData(
@Query("q") cityName:String
):Single<WeatherModel>
}`
weatherService
`//https://api.openweathermap.org/data/2.5/weather?
q=Omsk&units=metric&appid=34ae534ae4ff9829f963590e080ea518
class WeatherAPIservice {
private val BASE_URL ="https://api.openweathermap.org/"
private val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(WeatherAPI::class.java)
fun getDataService(cityName:String):Single<WeatherModel>{
return api.getData(cityName)
}
}`
weather.viewmodel
``` class MainViewModel: ViewModel(){
private val WeatherAPIservice = WeatherAPIservice()
private val disposable =CompositeDisposable()
val weather_data = MutableLiveData<WeatherModel>()
val weather_error = MutableLiveData<Boolean>()
val weather_load = MutableLiveData<Boolean>()
fun refreshData(cityName: String){
getDataFromAPI(cityName)
//getDataFromLocal()
}
private fun getDataFromAPI(cityName:String) {
weather_load.value =true
disposable.add(
WeatherAPIservice.getDataService(cityName)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: DisposableSingleObserver<WeatherModel>(){
override fun onSuccess(t: WeatherModel) {
weather_data.value=t
weather_error.value=false
weather_error.value=false
}
override fun onError(e: Throwable) {
weather_error.value=true
weather_load.value=false
}
})
)
}
}```

