android - 无法使用 Coroutines + RetroFit 获取数据
问题描述
我正在尝试使用协程 + 改造来获取数据。它显示 Class not found 异常这里是代码
类 MainActivity : AppCompatActivity(), CoroutineScope by MainScope(){
val key:String="47e0e7568fd74f8394e200926210211"
val q="London"
val days : Int=0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
launch (Dispatchers.Main) {
try {
val response =ApiAdapter.apiServices.getUserDetails(key,q,days,"no","no")
if (response.isSuccessful && response.body() !=null){
val data =response.body()!!
data.apply {
nameShowingView.text = location?.name
temp_cShowingView.text = current?.tempC.toString()
temp_FShowing.text = current?.tempF.toString()
WindMphShowing.text = current?.windMph.toString()
WindKphShowing.text = current?.windKph.toString()
WindDir.text = current?.windDir
FeelsLikeCShowing.text = current?.feelslikeC.toString()
FeelsLike_FShowing.text = current?.feelslikeF.toString()
DateShowing.text = forecast?.forecastday?.get(1)?.date
TempMaxShowing.text =
forecast?.forecastday?.get(1)?.day?.maxtempF.toString()
TempMinShowing.text =
forecast?.forecastday?.get(1)?.day?.mintempF.toString()
SunriseShowing.text = forecast?.forecastday?.get(1)?.astro?.sunrise
}
}else{
Toast.makeText(this@MainActivity,"Error Occurreddd :${response.message()}",Toast.LENGTH_SHORT).show()
}
} catch (e: Exception){
Toast.makeText(this@MainActivity,"Error Occurred: ${e.message}",Toast.LENGTH_SHORT).show()
}
}
}
}
对象 ApiAdapter {
val apiServices: ApiServices = Retrofit.Builder()
.baseUrl("http://api.weatherapi.com/v1/")
.client(OkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiServices::class.java)
}
接口 ApiServices {
@GET("/forecast.json")
suspend fun getUserDetails(@Query("key") key:String,
@Query("q") q:String,
@Query("days") days: Int,
@Query("aqi") aqi : String,
@Query("alerts") alerts:String
) : Response<ResponseDTO>
}
错误线...
在 void okhttp3.internal.platform.Platform.() (Platform.kt:170) 在 void okhttp3.OkHttpClient.(okhttp3.OkHttpClient$Builder) (OkHttpClient.kt:237) I/etchingretrofi: 在 void okhttp3.OkHttpClient.( ) (OkHttpClient.kt:222) at void com.example.weatherfetchingretrofit.ApiAdapter.() (ApiAdapter.kt:11) at java.lang.Object com.example.weatherfetchingretrofit.MainActivity$onCreate$1.invokeSuspend(java.lang. Object) (MainActivity.kt:26) at void kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(java.lang.Object) (ContinuationImpl.kt:33) at void kotlinx.coroutines.DispatchedTask.run() (DispatchedTask. kt:56) 在 void android.os.Handler.handleCallback(android.os.Message) (Handler.java:873) 在 void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:99)在 void android.os.Looper.loop() (Looper.java:201)
解决方案
推荐阅读
- google-cloud-platform - 直接访问 Google Cloud SQL 还是通过 pub 子队列访问?
- postgresql - `RETURN TABLE(columns)` 怎么能等同于使用一个或多个 `OUT` 参数,并将函数标记为返回 `SETOF` 记录?
- windows - Windows Virtualhost 上的 Apache:可在 IE 或 Edge 上运行,但无法在 Chrome 和 Firefox 上连接
- javascript - 拖动窗口
- qt - Qt Quick 动态自定义布局(如 IDE 中的工作区)
- java - 位置处缺少值分隔符
- javascript - 在 Angular 中动态修改 DOM
- python - 多处理池和生成器
- mysql - 如果不存在如何插入数据,否则更新它?
- java - Java-检查表中的数据集是否已经存在