Android 使用rxjava对2.0进行改造,即使使用subscribeOn()也会在主线程中进行代价高昂的反射
改装返回可见的接口Android 使用rxjava对2.0进行改造,即使使用subscribeOn()也会在主线程中进行代价高昂的反射,android,retrofit,Android,Retrofit,改装返回可见的接口 interface WeatherApi { companion object { val HOST = "https://api.heweather.com/x3/" val KEY = "41054a8f1d1a4ac992b1683e47a50146" } @GET("weather") fun getWeather(@Query("city") city: String, @Query("key") key: String)
interface WeatherApi {
companion object {
val HOST = "https://api.heweather.com/x3/"
val KEY = "41054a8f1d1a4ac992b1683e47a50146"
}
@GET("weather")
fun getWeather(@Query("city") city: String, @Query("key") key: String) : Observable<Weather>
}
当第一次调用firecode时,需要花费3秒钟
在主线程中反映事物,我很困惑,因为我使用了
subscribeOn(Schedulers.io())
我没有足够的声誉来发布图片,请参阅下面链接中的跟踪图片
当我改变改型接口以返回“Call”并在RestApi中用Observable包装响应时,一切都是正确的
改装返回调用的接口
interface WeatherApi {
companion object {
val HOST = "https://api.heweather.com/x3/"
val KEY = "41054a8f1d1a4ac992b1683e47a50146"
}
@GET("weather")
fun getWeather(@Query("city") city: String, @Query("key") key: String) : Call<Weather>
接口API{
伴星{
val主机=”https://api.heweather.com/x3/"
val KEY=“41054a8f1d1a4ac992b1683e47a50146”
}
@得到(“天气”)
fun getWeather(@Query(“city”)city:String,@Query(“key”)key:String):调用
}
在RestApi中创建可观察的我自己:
fun getWeatherData(city: String, key: String): Observable<Weather> {
return Observable.fromCallable { weatherApi.getWeather("qingdao",WeatherApi.KEY).execute().body() }
}
fun getWeatherData(城市:字符串,关键字:字符串):可观察{
返回Observable.fromCallable{weatherApi.getWeather(“青岛”,weatherApi.KEY).execute().body()}
}
然后调用fire代码,所有昂贵的事情都将在Schedulers.io()线程中完成
我没有足够的声誉来发布图片,请参阅下面链接中的跟踪图片
我的问题是:
这是马车吗
为什么直接返回Observable会导致此错误
或者改型有什么不对吗?如果这似乎是您的情况,请告诉我,我目前可以提供一个非常简单的解决方案
interface WeatherApi {
companion object {
val HOST = "https://api.heweather.com/x3/"
val KEY = "41054a8f1d1a4ac992b1683e47a50146"
}
@GET("weather")
fun getWeather(@Query("city") city: String, @Query("key") key: String) : Call<Weather>
fun getWeatherData(city: String, key: String): Observable<Weather> {
return Observable.fromCallable { weatherApi.getWeather("qingdao",WeatherApi.KEY).execute().body() }
}