改造网络响应在Rxjava2中返回null

时间:2020-02-29 14:52:19

标签: android kotlin rx-java2

***这是代码,我的响应返回null,我正在使用RXjav2可观察的实时数据。我想了解最新信息,我的位置是拉各斯,在改造界面中可以查询到。我正在使用天气堆栈API

2020-02-29 15:49:10.309 17988-17988 / com.example.weatherapp D / networks:空


  data class Weather (
      @SerializedName("name")
      val location: String,
      @SerializedName("weather_descriptions")
      val description: List<String>,
      @SerializedName("temperature")
      val temperature: Long,
      @SerializedName("wind_speed")
      val windSpeed: Long,
      @SerializedName("wind_dir")
      val windDir: String,
      @SerializedName("precip")
      val precipitation: Long,
      @SerializedName("humidity")
      val humidity: Long,
      @SerializedName("visibility")
      val visibility: Long,
      @SerializedName("weather_icons")
      val icon: List<String>
  ): Parcelable



  data class Request(
      @SerializedName("language")
      val language: String,
      @SerializedName("query")
      val query: String,
      @SerializedName("type")
      val type: String,
      @SerializedName("unit")
      val unit: String
  ) 



data class Location(
      @SerializedName("country")
      val country: String,
      @SerializedName("lat")
      val lat: String,
      @SerializedName("localtime")
      val localtime: String,
      @SerializedName("localtime_epoch")
      val localtime_epoch: Int,
      @SerializedName("lon")
      val lon: String,
      @SerializedName("name")
      val name: String,
      @SerializedName("region")
      val region: String,
      @SerializedName("timezone_id")
      val timezone_id: String,
      @SerializedName("utc_offset")
      val utc_offset: String
  )


响应数据类

 data class WeatherResponse(
    @SerializedName("current")
    val current: Weather,
    @SerializedName("location")
    val location: Location,
    @SerializedName("request")
    val request: Request
)

网络接口

   interface ApixuApi {
       @GET("current///")
       fun getWeather(@Query("access_key") api: String = Util.API_KEY,
                          @Query("query") location: String = "Lagos"): Observable<WeatherResponse> 




数据源界面

interface DataSource {

    fun getWeather(): LiveData<WeatherResponse>

    fun refreshWeather()

    fun saveWeather(weather: Weather)

    fun clearWeather()

}


 package com.example.weatherapp.dataSource

 import android.util.Log
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import com.example.weatherapp.model.Weather
 import com.example.weatherapp.model.WeatherResponse
 import com.example.weatherapp.network.ApixuApi
 import com.example.weatherapp.util.NetworkState
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.disposables.CompositeDisposable
 import io.reactivex.schedulers.Schedulers

 class RemoteDataSource(private val api: ApixuApi, private val compositeDisposable: CompositeDisposable) : DataSource {

     private val _networkState = MutableLiveData<NetworkState>()
     val networkState: LiveData<NetworkState>
     get() = _networkState

     private val _apiResponse = MutableLiveData<WeatherResponse>()
     val apiResponse
     get() = _apiResponse

     override fun getWeather(): LiveData<WeatherResponse> {
         _networkState.postValue(NetworkState.LOADING)

         var data:Weather? = null

         try {
             compositeDisposable.add(
                 api.getWeather()
                     .observeOn(AndroidSchedulers.mainThread())
                     .subscribeOn(Schedulers.io())
                     .subscribe(
                         {
                         //  data = _apiResponse.value.current
                           // data =  it
                            //data =  _apiResponse.value!!.current
                             data = _apiResponse.value!!.current
                             Log.d("remotedd", _apiResponse.value!!.current.toString())
                             _networkState.postValue(NetworkState.LOADED)
                         },
                         {
                             _networkState.postValue(NetworkState.ERROR)
                             Log.d("networks", it.message.toString())
                         }
                     )
             )

         }catch (e: Exception){
             Log.d("networks", e.message.toString())
         }
         return apiResponse
     }

     override fun refreshWeather() {
         _apiResponse.value = getWeather().value

     }

     override fun saveWeather(weather: Weather) {
         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
     }

     override fun clearWeather() {
         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
     }


 }

我需要有关如何解决此问题的帮助,我的回答为空

0 个答案:

没有答案