我想将以下Volley字符串请求迁移到Retrofit2。该请求检索一个响应主体作为String,我对此进行了解析。
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做这样一个简单的请求?
实际上,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()
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句