Skip to content

Instantly share code, notes, and snippets.

@kohendrix
Created February 2, 2019 04:05
Show Gist options
  • Select an option

  • Save kohendrix/3b0c9d305660ddfdafab4e8643edc3aa to your computer and use it in GitHub Desktop.

Select an option

Save kohendrix/3b0c9d305660ddfdafab4e8643edc3aa to your computer and use it in GitHub Desktop.
Volley Coroutines Sample request() no coroutines
fun request(method: Int, url: String, headers: Map<String, String>, jsonRequest: JSONObject?,
successListener: (json: JSONObject) -> Unit, errorListener: (error: Exception) -> Unit) {
val request = object : JsonObjectRequest(method, url, jsonRequest,
Response.Listener<JSONObject> { json -> successListener(json) },
Response.ErrorListener { e -> errorListener(e) }) {
override fun getHeaders(): Map<String, String> = headers
override fun parseNetworkError(volleyError: VolleyError): VolleyError {
return volleyError.networkResponse?.let {
Log.e("VolleyRequest", volleyError.networkResponse.headers.toString())
Log.e("VolleyRequest", String(volleyError.networkResponse.data))
VolleyError(String(volleyError.networkResponse.data))
} ?: volleyError
}
}
request.retryPolicy = DefaultRetryPolicy(5000, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
try {
Volley.newRequestQueue(applicationContext).addToRequestQueue(request) // must be singleton!!
} catch (e: Exception) {
errorListener(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment