Skip to content

Instantly share code, notes, and snippets.

@TheLittleNaruto
Created April 19, 2016 05:43
Show Gist options
  • Save TheLittleNaruto/928ff53c7f4cf82f1ee97acd271f3ed7 to your computer and use it in GitHub Desktop.
Save TheLittleNaruto/928ff53c7f4cf82f1ee97acd271f3ed7 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fussy.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
public class MyVolleyRequest implements Response.ErrorListener, Response.Listener<String> {
private static final int MY_SOCKET_TIMEOUT_MS = 6 * 1000;
private static MyVolleyRequest myVolleyRequest;
private Context mContext;
ResponseListener mListener;
public MyVolleyRequest(Context context) {
mContext = context;
}
public interface ResponseListener {
public void onResponse(JSONObject mJsonObject);
public void onError(String error);
}
public static synchronized MyVolleyRequest getInstance(Context context) {
if (myVolleyRequest == null) {
myVolleyRequest = new MyVolleyRequest(context);
}
return myVolleyRequest;
}
public void setResponseListener(ResponseListener listener) {
mListener = listener;
}
public void addRequest(final Map<String, String> param, String url) {
StringRequest request = new StringRequest(Request.Method.POST, url, this, this) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return param;
}
};
request.setShouldCache(false);
request.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MyVolleySingleton.getInstance(mContext).addToRequestQueue(request);
}
public void addGetRequest(String url) {
StringRequest request = new StringRequest(Request.Method.GET, url, this, this);
request.setShouldCache(false);
request.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MyVolleySingleton.getInstance(mContext).addToRequestQueue(request);
}
@Override
public synchronized void onErrorResponse(VolleyError volleyError) {
if (mListener != null) {
volleyError.printStackTrace();
mListener.onError(mContext.getString(R.string.server_error));
MyVolleySingleton.getInstance(mContext).getRequestQueue().getCache().clear();
}
}
@Override
public synchronized void onResponse(String s) {
try {
Log.d("VERIFICATION_RESPONSE", s);
if (mListener != null) {
mListener.onResponse(new JSONObject(s));
MyVolleySingleton.getInstance(mContext).getRequestQueue().getCache().clear();
}
} catch (JSONException e) {
e.printStackTrace();
mListener.onError("JSONException=>" + e.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment