我想将以下Volley字符串请求迁移到Retrofit2。此请求检索字符串形式的响应体,我自己对其进行解析。
fun updatePodcastEpisodesRQ( url: String) {
val feedReq = StringRequestUTF8(
Request.Method.GET,
url,
{ response: String? -> ...},
{ error: VolleyError ->...}
)
App.instance?.addToRequestQueue(feedReq, TAG_JSON_REQUEST1)
} 请注意,URL可以是任何地址,因此在执行JSON请求时没有Retrofit.Builder()中定义的baseUrl。
有没有可能用Retrofit2做这么简单的请求呢?
发布于 2021-02-28 06:23:56
事实上,okhttp3满足了我所有的需求。我从Volley迁移到了okhttp3。
private val client = OkHttpClient()
suspend fun getFeed(url: String): String {
val request = Request.Builder()
.url(url)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful)
throw IOException("Error occurred - code:${response.code} message=${response.message}")
if (response.body == null)
throw IOException("Error occurred - null body")
return response.body!!.string()
}
}https://stackoverflow.com/questions/66387358
复制相似问题