u2gilles
u2gilles

Reputation: 7393

Volley to Retrofit2 - How to do a String Request with Retrofit2?

I would like to migrate to Retrofit2 the following Volley string request. This request retrieves a response body as String, which I parse myself.

fun updatePodcastEpisodesRQ( url: String) {
   val feedReq = StringRequestUTF8(
     Request.Method.GET,
     url,
     { response: String? -> ...},
     { error: VolleyError ->...}
   )
  App.instance?.addToRequestQueue(feedReq, TAG_JSON_REQUEST1)
}   

Please note that the URL can be any address, as a result there is no baseUrl as defined in Retrofit.Builder() when doing JSON request for example.

Is it possible to do such a simple request with Retrofit2 ?

Upvotes: 1

Views: 147

Answers (1)

u2gilles
u2gilles

Reputation: 7393

In fact, okhttp3 fulfilled all my needs. I migrated from Volley to 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()
    }
}

Upvotes: 0

Related Questions