Created
March 23, 2016 17:39
-
-
Save frankibem/134307a610d04e06a3c2 to your computer and use it in GitHub Desktop.
Sample volley singleton
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
package com.test.testjson; | |
import android.content.Context; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.toolbox.Volley; | |
/** | |
* @author Frank Ibem. | |
*/ | |
public class VolleySingleton { | |
private static VolleySingleton mInstance; | |
private RequestQueue mRequestQueue; | |
private static Context mCtx; | |
private VolleySingleton(Context context) { | |
mCtx = context; | |
mRequestQueue = getRequestQueue(); | |
} | |
public static synchronized VolleySingleton getInstance(Context context) { | |
if (mInstance == null) { | |
mInstance = new VolleySingleton(context); | |
} | |
return mInstance; | |
} | |
public RequestQueue getRequestQueue() { | |
if (mRequestQueue == null) { | |
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); | |
} | |
return mRequestQueue; | |
} | |
public <T> void addToRequestQueue(Request<T> req) { | |
getRequestQueue().add(req); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment