【问题标题】:Volley to Retrofit2 - How to do a String Request with Retrofit2?Volley to Retrofit2 - 如何使用 Retrofit2 进行字符串请求?
【发布时间】:2021-05-28 22:12:14
【问题描述】:

我想将以下 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 可以做这么简单的请求吗?

【问题讨论】:

    标签: android retrofit2 android-volley


    【解决方案1】:

    事实上,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()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-14
      • 2017-02-13
      • 2021-10-05
      • 2018-01-09
      • 1970-01-01
      • 2018-02-13
      • 2016-07-31
      • 2017-09-30
      相关资源
      最近更新 更多