Created
December 25, 2015 17:49
-
-
Save akkefa/36d6ef6c57608c5dcb5c to your computer and use it in GitHub Desktop.
Google Volley starter Kit
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
package com.example.ikram.bathak.network; | |
import android.graphics.Bitmap; | |
import android.support.v4.util.LruCache; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.toolbox.ImageLoader; | |
import com.android.volley.toolbox.Volley; | |
import com.example.ikram.bathak.MyApplication; | |
public class VolleySingleton { | |
private static VolleySingleton mInstance = null; | |
private RequestQueue mRequestQueue; | |
private ImageLoader mImageLoader; | |
private VolleySingleton(){ | |
mRequestQueue = Volley.newRequestQueue(MyApplication.getAppContext()); | |
mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() { | |
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(getDefaultLruCacheSize()); | |
public void putBitmap(String url, Bitmap bitmap) { | |
mCache.put(url, bitmap); | |
} | |
public Bitmap getBitmap(String url) { | |
return mCache.get(url); | |
} | |
}); | |
} | |
public static VolleySingleton getInstance(){ | |
if(mInstance == null){ | |
mInstance = new VolleySingleton(); | |
} | |
return mInstance; | |
} | |
public RequestQueue getRequestQueue(){ | |
return this.mRequestQueue; | |
} | |
public ImageLoader getImageLoader(){ | |
return this.mImageLoader; | |
} | |
private static int getDefaultLruCacheSize() { | |
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); | |
return maxMemory / 8; | |
} | |
} |
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
package com.example.ikram.bathak.network; | |
import android.util.Log; | |
import com.android.volley.DefaultRetryPolicy; | |
import com.android.volley.Response; | |
import com.android.volley.toolbox.StringRequest; | |
public class WebClient extends StringRequest { | |
Priority priorityRequest = Priority.NORMAL; | |
public WebClient(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) { | |
super(url, listener, errorListener); | |
setRetryPolicy(new DefaultRetryPolicy(5000 , 3 , DefaultRetryPolicy.DEFAULT_BACKOFF_MULT ) ); | |
VolleySingleton.getInstance().getRequestQueue().add(this); | |
} | |
public WebClient(String url, Response.Listener<String> listener, Response.ErrorListener errorListener , Priority priority) | |
{ | |
super(url, listener, errorListener); | |
setPriority(priority); | |
setRetryPolicy(new DefaultRetryPolicy(5000, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); | |
VolleySingleton.getInstance().getRequestQueue().add(this); | |
} | |
@Override | |
protected void deliverResponse(String response) { | |
super.deliverResponse(response); | |
Log.i("Volley Request:", priorityRequest.toString() + " => " + getUrl()); | |
} | |
public void setPriority(Priority priority) | |
{ | |
priorityRequest = priority; | |
} | |
/** | |
* Returns the {@link Priority} of this request; {@link Priority#NORMAL} by default. | |
*/ | |
@Override | |
public Priority getPriority() { | |
return priorityRequest; | |
} | |
} | |
/* | |
new WebClient( URL.buildUrl( WebUrl.POST_URL, "20", null, null, postsDataSource.getLastPostWebIdByType( "video" , false , null ), null, "video" ), new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse( String response ) | |
{ | |
final String r = response; | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
List<Post> postList ; | |
postList = WebJSONParser.postParseFeed( r ); | |
postsDataSource.insertPosts(postList); | |
startActivityRequestDone(); | |
} | |
}).start(); | |
} | |
}, new Response.ErrorListener() | |
{ | |
@Override | |
public void onErrorResponse( VolleyError error ) | |
{ | |
Log.d( TAG, "POST Request failed " + error.getMessage() ); | |
startActivityRequestDone(); | |
} | |
}, Request.Priority.LOW ).setTag(TAG); | |
protected void onPause() | |
{ | |
super.onPause(); | |
postsDataSource.close(); | |
VolleySingleton.getInstance().getRequestQueue().cancelAll(TAG); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment