首页 > 解决方案 > 无法使用 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)

标签: androidkotlinretrofit2kotlin-coroutinescoroutine

解决方案


推荐阅读