android - 为什么我使用的weatherstack api返回null?
问题描述
我不明白为什么我的天气堆栈 api 响应在以下代码中返回 null。请帮忙
这是我的模型数据类:
当前天气响应.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
)
位置.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
)
请求.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
注意: 我还向您提到的一件事我正在使用天气堆栈 API 的免费计划,它不允许我使用 https 订阅,所以我使用 http ..... Https 错误
解决方案
主要有两件事要改变,他们发送 API 密钥的密钥是错误的。它应该access_key
代替key
val url = chain.request()
.url()
.newBuilder()
.addQueryParameter("access_key", API_KEY)
.build()
其次,正如我所说,您的数据模型有点错误。所以你可以再次确认。特别是更改precip
为Double
推荐阅读
- c# - 反转while循环
- r - 使用 RSTUDIO 对 SpaceX 工作进行网络抓取
- python - 如何使用 dask 从 S3 读取 csv 文件并提及我的访问密钥和秘密?
- c++ - 使用 /clr:netcore 从非托管 C++ 调用 .dll 构建
- angular - 使用角度材质网格列表时如何避免水平滚动条?
- javascript - 使用“Ctrl+A”全选并删除输入字段的内容
- docker - 在 docker 中附加卷后找不到模块
- swift - Swift Firestore 分页正在检索下一批文档但替换第一批
- javascript - 使用Javascript为对象数组中的每个对象添加一个递增的数字
- jquery - 你可以在 fadeIn Jquery 之后嵌入 SVG 的链接吗?