Kotlin Coroutines,改造版本2.6.1,网络响应为空

时间:2019-10-29 11:05:34

标签: android-studio request kotlin-coroutines retrofit2.6

我正在阅读reso-coder Weather app教程。随着时间的流逝,大多数事情都发生了变化,apixu天气网站也随之变化。
现在是Retrofit 2.6.1的时间,这意味着kotlin coroutines
问题是我在网络响应中得到的所有内容都为空
我遍历了所有带有Seri​​alizedName的数据类,一切看起来都还不错,仍然无法解决问题。.
顺便说一句,我现在不使用ViewModel只是直接跳进片段textView

interface ApixuWeatherApiService {


 @GET("current")
    suspend fun getCurrentWeather(
        @Query("query") location: String,
        @Query("lang") languageCode: String = "en"
    ): CurrentWeatherResponse

    //to handle this above interface we need companion object

    companion object WeatherAPis {
        private val requestInterceptor = Interceptor { chain ->
            val url = chain.request()
                .url().newBuilder().addQueryParameter("access_key", API_KEY)
                .build()

            val request = chain.request().newBuilder().url(url).build()
            chain.proceed(request)
        }
        private val okHTTPClient = OkHttpClient.Builder().addInterceptor(requestInterceptor).build()
        private fun retroFit(): Retrofit = Retrofit
            .Builder()
            .client(okHTTPClient)
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val weatherApi: ApixuWeatherApiService =
            retroFit().create(ApixuWeatherApiService::class.java)
    }
}

片段类


class CurrentWeatherFragment : Fragment() {


    private lateinit var viewModel: CurrentWeatherViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.current_weather_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(CurrentWeatherViewModel::class.java)
        CoroutineScope(IO).launch {
            widCOntext()
        }

    }


    private suspend fun widCOntext() {
        val apiService = ApixuWeatherApiService.weatherApi
            withContext(Main) {
                val currentWeatherResponse =
                    withContext(IO) {
                        apiService.getCurrentWeather(
                            "London"
                        )
                    }
                txtView.text = currentWeatherResponse.toString()
            }

    }
}

我已经使用插件将JSON转换为kotlin文件来获取这些
四个数据类
当前

data class Current(
    @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
)

CurrentWeatherResponse

data class CurrentWeatherResponse(
    val request: Request,
    val location: Location,
    val current: Current
)

位置

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
)

请求

data class Request(
    val type: String,
    val query: String,
    val language: String,
    val unit: String
)

1 个答案:

答案 0 :(得分:0)

问题已解决:我希望这能对某人有所帮助
这是我做的
每当我停留在某个地方时,我都会创建新项目并尝试处理该特定事项。
现在我在这里所做的一切都解决了我的问题。
我使用了singleton模式,已经创建了 ApiService接口的Singleton实例,在 singleton 之前,它显示了响应200,但输出也为 null ,并且现在只需制作 singleton 即可解决我的问题,现在我得到了期望的输出
这是代码:

std::vector<std::vector<double>> A(300, std::vector<double>(500));
std::vector<std::vector<double>> subranges;

subranges.reserve(11 - 5);
std::transform(A.begin() + 5, A.begin() + 11,
               std::back_inserter(subranges),
               [](const auto& inner){ // [](const std::vector<double>& inner) {
                   return std::vector<double>(inner.begin() + 25,
                                              inner.begin() + 101);
               });

具体地说,这是我正在谈论的部分

object RetrofitRequest {

    private val interceptor = Interceptor {chain ->
        val url = chain.request()
            .url()
            .newBuilder()
            .addQueryParameter("access_key/key", yourApiKey)
            .build()

        val request = chain.request().newBuilder().url(url).build()

        return@Interceptor chain.proceed(request)
    }

    private val okHttpClient = OkHttpClient
        .Builder()
        .addInterceptor(interceptor)
        .build()

    @Volatile
    private var instance : ApiService? = null
    fun getInstance() : ApiService = instance ?: synchronized(this) {
        instance ?: fullResponse().also {
            instance = it
        }
    }

    private fun retrofitBuild() : Retrofit =
        Retrofit.Builder()
           .client(okHttpClient)
           .baseUrl(BASE_URL)
           .addConverterFactory(GsonConverterFactory.create())
            .build()


    private fun fullResponse(): ApiService {
        return retrofitBuild().create(ApiService::class.java)
    }

}

现在我认为这里发生了什么:
Singleton 之前,响应已成功,但其他线程看不到实例。这就是为什么 null 。当我使用协程时,在使 singleton @volatile 使得所有线程可见 singleton 之后,它可能从不同的线程运行。当程序尝试从其他线程运行并且 @Volatile 具有访问所有线程的功能时,程序可以成功执行而无需 null