Last active
November 18, 2017 16:57
-
-
Save dcz-switcher/c1d535fcf4bff473bf64e520197e66de to your computer and use it in GitHub Desktop.
snippets Kotlin
This file contains 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
// ------- HOW DEFINE A SINGLETON FOR VOLLEY REQUEST ------- | |
// original source in Java: https://developer.android.com/training/volley/requestqueue.html | |
object MySingleton { | |
val TAG:String = "MySingleton" | |
var mRequestQueue:RequestQueue? = null | |
init { | |
Log.d(TAG, "init") | |
} | |
fun getRequestQueue(context:Context):RequestQueue? { | |
Log.d(TAG, "getRequestQueue") | |
if(mRequestQueue == null) { | |
mRequestQueue = Volley.newRequestQueue(context) | |
} | |
return mRequestQueue | |
} | |
fun <T> addToRequestQueue(req:Request<T>){ | |
mRequestQueue?.add(req) | |
} | |
} | |
// ------- MAKE VOLLEY REQUEST WITH SINGLETON ------- | |
val url:String = "https://ringsdb.com/api/public/card/01002" | |
val jsonObjectRequest: JsonObjectRequest= JsonObjectRequest(Request.Method.GET, url, null, | |
Response.Listener { | |
response:JSONObject -> Log.d(TAG, response.toString()) | |
}, | |
Response.ErrorListener { | |
error:VolleyError -> Log.e(TAG, error.message) | |
} | |
) | |
MySingleton.getRequestQueue(this) | |
MySingleton.addToRequestQueue(jsonObjectRequest) | |
//----- START NEW ACTIVITY WITH INTENT ------ | |
var intent:Intent = Intent(applicationContext, OtherActivity::class.java) | |
startActivity(intent) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment