首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Retrofit2 to Retrofit2 -如何使用Volley进行字符串请求?

Retrofit2 to Retrofit2 -如何使用Volley进行字符串请求?
EN

Stack Overflow用户
提问于 2021-02-26 22:12:48
回答 1查看 46关注 0票数 1

我想将以下Volley字符串请求迁移到Retrofit2。此请求检索字符串形式的响应体,我自己对其进行解析。

代码语言:javascript
复制
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做这么简单的请求呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-28 06:23:56

事实上,okhttp3满足了我所有的需求。我从Volley迁移到了okhttp3。

代码语言:javascript
复制
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()
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66387358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档