为什么 weatherstack api 我使用返回 null?
why weatherstack api I used returning null?
我不明白为什么我的天气堆栈 api 响应 return 在以下代码中为空。请帮忙
这是我的模型数据类:
CurrentWeatherResponse.kt
data class CurrentWeatherResponse(
val request: Request,
val location: Location,
@SerializedName(value = "current")
val currentWeatherEntry: CurrentWeatherEntry
)
CurrentWeatherEntry.kt
data class CurrentWeatherEntry(
@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
)
Location.kt
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
)
Request.kt
data class Request(
val type: String,
val query: String,
val language: String,
val unit: String
)
这是我的 WeatherStack API 服务代码:
WeatherStackApiService.kt
const val API_KEY= "04246030f0fc7b1efe5a28d1c1724b1e"
//http://api.weatherstack.com/current?access_key=04246030f0fc7b1efe5a28d1c1724b1e&query=New%20York
interface WeatherStackApiService {
@GET(value = "current")
fun getCurrentWeatherAsync(
@Query(value = "query") location: String
// @Query(value = "language") languageCode :String = "en"
): Deferred<CurrentWeatherResponse>
companion object {
operator fun invoke(): WeatherStackApiService
{
val requestInterceptor = Interceptor { chain ->
val url = chain.request()
.url()
.newBuilder()
.addQueryParameter("key", API_KEY)
.build()
val request = chain.request()
.newBuilder()
.url(url)
.build()
return@Interceptor chain.proceed(request)
}
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(requestInterceptor)
.build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("http://api.weatherstack.com/")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WeatherStackApiService::class.java)
}
}
}
这是显示该地点天气响应的 CurrentWeatherFragment 文件:
CurrentWeatherFragment.kt
class CurrentWeatherFragment : Fragment() {
companion object{
fun newInstance() = CurrentWeatherFragment()
}
private lateinit var currentWeatherViewModel: CurrentWeatherViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
currentWeatherViewModel =
ViewModelProvider(this).get(CurrentWeatherViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
val apiService = WeatherStackApiService()
GlobalScope.launch(Dispatchers.Main) {
val currentWeatherResponse = apiService.getCurrentWeatherAsync("London").await()
textView.text = currentWeatherResponse.toString()
}
return root
}
}
这是我们 运行 该应用程序时的输出:
Output ScreenShot of the app
注:
我还向您提到的一件事是我正在使用 weatherstack API 的免费计划,它不允许我使用 https 订阅,所以我使用 http.....
Https error
主要有两点需要更改,您发送 API 密钥的密钥是错误的。
它应该是 access_key
而不是 key
val url = chain.request()
.url()
.newBuilder()
.addQueryParameter("access_key", API_KEY)
.build()
其次,正如我所说,您的数据模型有点不对。所以你可以再确认一下。特别是将 precip
更改为 Double
我不明白为什么我的天气堆栈 api 响应 return 在以下代码中为空。请帮忙
这是我的模型数据类:
CurrentWeatherResponse.kt
data class CurrentWeatherResponse(
val request: Request,
val location: Location,
@SerializedName(value = "current")
val currentWeatherEntry: CurrentWeatherEntry
)
CurrentWeatherEntry.kt
data class CurrentWeatherEntry(
@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
)
Location.kt
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
)
Request.kt
data class Request(
val type: String,
val query: String,
val language: String,
val unit: String
)
这是我的 WeatherStack API 服务代码:
WeatherStackApiService.kt
const val API_KEY= "04246030f0fc7b1efe5a28d1c1724b1e"
//http://api.weatherstack.com/current?access_key=04246030f0fc7b1efe5a28d1c1724b1e&query=New%20York
interface WeatherStackApiService {
@GET(value = "current")
fun getCurrentWeatherAsync(
@Query(value = "query") location: String
// @Query(value = "language") languageCode :String = "en"
): Deferred<CurrentWeatherResponse>
companion object {
operator fun invoke(): WeatherStackApiService
{
val requestInterceptor = Interceptor { chain ->
val url = chain.request()
.url()
.newBuilder()
.addQueryParameter("key", API_KEY)
.build()
val request = chain.request()
.newBuilder()
.url(url)
.build()
return@Interceptor chain.proceed(request)
}
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(requestInterceptor)
.build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("http://api.weatherstack.com/")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WeatherStackApiService::class.java)
}
}
}
这是显示该地点天气响应的 CurrentWeatherFragment 文件:
CurrentWeatherFragment.kt
class CurrentWeatherFragment : Fragment() {
companion object{
fun newInstance() = CurrentWeatherFragment()
}
private lateinit var currentWeatherViewModel: CurrentWeatherViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
currentWeatherViewModel =
ViewModelProvider(this).get(CurrentWeatherViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
val apiService = WeatherStackApiService()
GlobalScope.launch(Dispatchers.Main) {
val currentWeatherResponse = apiService.getCurrentWeatherAsync("London").await()
textView.text = currentWeatherResponse.toString()
}
return root
}
}
这是我们 运行 该应用程序时的输出: Output ScreenShot of the app
注: 我还向您提到的一件事是我正在使用 weatherstack API 的免费计划,它不允许我使用 https 订阅,所以我使用 http..... Https error
主要有两点需要更改,您发送 API 密钥的密钥是错误的。
它应该是 access_key
而不是 key
val url = chain.request()
.url()
.newBuilder()
.addQueryParameter("access_key", API_KEY)
.build()
其次,正如我所说,您的数据模型有点不对。所以你可以再确认一下。特别是将 precip
更改为 Double