Skip to content

Instantly share code, notes, and snippets.

@shihabmi7
Last active December 4, 2017 08:24
Show Gist options
  • Save shihabmi7/9df7fbbebd4d51624d0b0a2de0119d88 to your computer and use it in GitHub Desktop.
Save shihabmi7/9df7fbbebd4d51624d0b0a2de0119d88 to your computer and use it in GitHub Desktop.
Volley Singletone Class : Multipart
private void submitNLPInfo(final String nlpInfo) {
VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, "URL HERE", new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
//params.put("phone",MobileNumber);
params.put("nid", nlpInfo);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
LogMe.d("acc::", DriverAccToken);
params.put("authorization", DriverAccToken);
return params;
}
@Override
protected Map<String, DataPart> getByteData() {
Map<String, DataPart> params = new HashMap<>();
// file name could found file base or direct access from real path
// for now just get bitmap data from ImageView
params.put("nlp_image", new DataPart("nlp_avatar.jpg", AppHelper.getFileDataFromDrawable(getActivity(), ivNidPassport.getDrawable()), "image/jpeg"));
return params;
}
};
VolleySingleton.getInstance(getActivity()).addToRequestQueue(multipartRequest);
}
package com.muvasia.driver.extras;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
/**
* Private constructor, only initialization from getInstance.
*
* @param context parent context
*/
private VolleySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruBitmapCache(5000);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
/**
* Singleton construct design pattern.
*
* @param context parent context
* @return single instance of VolleySingleton
*/
public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}
/**
* Get current request queue.
*
* @return RequestQueue
*/
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
/**
* Add new request depend on type like string, json object, json array request.
*
* @param req new request
* @param <T> request type
*/
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
/**
* Get image loader.
* @return ImageLoader
*/
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment