我正在阅读 reso-coderWeather app
教程。大多数事情随着时间的推移而改变,apixu天气网站也是如此。
现在是时候了,Retrofit 2.6.1
这意味着kotlin coroutines
问题是我在网络响应中得到了所有 Null
我已经使用 SerializedName 遍历了所有数据类,一切似乎都很好,仍然无法解决问题..
顺便说一句,我没有使用ViewModel
正确现在只是直接希望进入片段 textView
interface ApixuWeatherApiService {
@GET("current")
suspend fun getCurrentWeather(
@Query("query") location: String,
@Query("lang") languageCode: String = "en"
): CurrentWeatherResponse
//to handle this above interface we need companion object
companion object WeatherAPis {
private val requestInterceptor = Interceptor { chain ->
val url = chain.request()
.url().newBuilder().addQueryParameter("access_key", API_KEY)
.build()
val request = chain.request().newBuilder().url(url).build()
chain.proceed(request)
}
private val okHTTPClient = OkHttpClient.Builder().addInterceptor(requestInterceptor).build()
private fun retroFit(): Retrofit = Retrofit
.Builder()
.client(okHTTPClient)
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val weatherApi: ApixuWeatherApiService =
retroFit().create(ApixuWeatherApiService::class.java)
}
}
片段类
class CurrentWeatherFragment : Fragment() {
private lateinit var viewModel: CurrentWeatherViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.current_weather_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(CurrentWeatherViewModel::class.java)
CoroutineScope(IO).launch {
widCOntext()
}
}
private suspend fun widCOntext() {
val apiService = ApixuWeatherApiService.weatherApi
withContext(Main) {
val currentWeatherResponse =
withContext(IO) {
apiService.getCurrentWeather(
"London"
)
}
txtView.text = currentWeatherResponse.toString()
}
}
}
我已经使用插件将 JSON 转换为 kotlin 文件以获取这
四个数据类
Current
data class Current(
@SerializedName("observation_time")
val observationTime: String,
val temperature: Int,
@SerializedName("weather_code")
val weatherCode: Int,
@SerializedName("weather_icons")
val weatherIcons: List<String>,
@SerializedName("weather_descriptions")
val weatherDescriptions: List<String>,
@SerializedName("wind_speed")
val windSpeed: Int,
@SerializedName("wind_degree")
val windDegree: Int,
@SerializedName("wind_dir")
val windDir: String,
val pressure: Int,
val precip: Int,
val humidity: Int,
val cloudcover: Int,
val feelslike: Int,
@SerializedName("uv_index")
val uvIndex: Int,
val visibility: Int,
@SerializedName("is_day")
val isDay: String
)
当前天气响应
data class CurrentWeatherResponse(
val request: Request,
val location: Location,
val current: Current
)
地点
data class Location(
val name: String,
val country: String,
val region: String,
val lat: String,
val lon: String,
@SerializedName("timezone_id")
val timezoneId: String,
val localtime: String,
@SerializedName("localtime_epoch")
val localtimeEpoch: Int,
@SerializedName("utc_offset")
val utcOffset: String
)
要求
data class Request(
val type: String,
val query: String,
val language: String,
val unit: String
)