Skip to content

Instantly share code, notes, and snippets.

@jaonoctus
Created November 8, 2015 04:13
Show Gist options
  • Save jaonoctus/a9a158f5b8c456a083a7 to your computer and use it in GitHub Desktop.
Save jaonoctus/a9a158f5b8c456a083a7 to your computer and use it in GitHub Desktop.
melhoria volley by Matheus
package me.waymart.android.network;
import android.content.Context;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.error.AuthFailureError;
import com.android.volley.request.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
import me.waymart.android.App;
/**
* Helper class that is used to provide references to initialized RequestQueue(s)
*
* @author Matheus Godinho
*
*/
public class Network {
private static RequestQueue mRequestQueue;
private static Map<String, String> mHeaders = new HashMap<>();
private static Map<String, String> mParams = new HashMap<>();
private Network() {}
public static void init(Context c) {
mRequestQueue = Volley.newRequestQueue(c);
}
public static RequestQueue getRequestQueue() {
if (mRequestQueue != null) {
return mRequestQueue;
} else {
throw new IllegalStateException("RequestQueue not initialized");
}
}
/**
* Find request method by method on string
* @param m string method. Ex: GET, POST, PUT, DELETE
* @return method on int
*/
private static int getMethod(String m) {
switch (m) {
case "GET":
return Request.Method.GET;
case "POST":
return Request.Method.POST;
case "PUT":
return Request.Method.PUT;
case "DELETE":
return Request.Method.DELETE;
default:
return Request.Method.GET;
}
}
/**
* Add default headers to request
*/
private static void defaultHeaders() {
mHeaders.clear();
if (!App.USER.getSid().isEmpty()) {
mHeaders.put("x-session-id", App.USER.getSid());
}
}
/**
* Add default params to request
*/
private static void defaultParams() {
//mParams.put("email", "teste");
//mParams.put("senha", "seha");
}
/**
* Set param to make request
* @param param key
* @param value value
*/
public static void setParam(String param, String value) {
mParams.put(param, value);
}
/**
* Clear all params saved in class
*/
public static void clearParams() {
mParams.clear();
}
/**
* Make a HTTP request
* @param method HTTP method. Ex: GET or POST
* @param reqSuccessListener Success listener function
* @param reqErrorListener Error listener function
*/
public static void execute(String method, String url, Response.Listener<String> reqSuccessListener, Response.ErrorListener reqErrorListener) {
// Set default params and headers
defaultHeaders();
defaultParams();
// Get request queue
RequestQueue queue = getRequestQueue();
// Check method to send params
switch (method) {
case "GET":
mParams.clear();
break;
}
// Debug log
Log.d(App.DEBUG, "Network: URL -> " + url);
Log.d(App.DEBUG, "Network: METHOD -> " + method);
for(Map.Entry<String, String> entry : mHeaders.entrySet()) {
Log.d(App.DEBUG, "Network: HEADER -> " + entry.getKey()+": "+entry.getValue());
}
for(Map.Entry<String, String> entry : mParams.entrySet()) {
Log.d(App.DEBUG, "Network: PARAM -> " + entry.getKey()+": "+entry.getValue());
}
// Make a request
StringRequest req = new StringRequest(getMethod(method),
url,
reqSuccessListener,
reqErrorListener) {
// Headers to send on request
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
// Check if has headers
if (mHeaders != null) {
return mHeaders;
} else {
return null;
}
}
// Params to send on request
@Override
protected Map<String, String> getParams() {
// Check if has params
if (mParams != null) {
return mParams;
} else {
return null;
}
}
};
// Disable cache on request
req.setShouldCache(false);
// Add request to the queue
queue.add(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment