Created
February 2, 2019 04:05
-
-
Save kohendrix/3b0c9d305660ddfdafab4e8643edc3aa to your computer and use it in GitHub Desktop.
Volley Coroutines Sample request() no coroutines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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