Skip to content

Instantly share code, notes, and snippets.

@vishalforcode
Last active April 28, 2017 08:10
Show Gist options
  • Save vishalforcode/abec6b94639ae9f3f4982a549418bd4f to your computer and use it in GitHub Desktop.
Save vishalforcode/abec6b94639ae9f3f4982a549418bd4f to your computer and use it in GitHub Desktop.
Volley Helper Class
//In App Module Build.gradle .. import volley dependency as
// compile 'com.android.volley:volley:1.0.0'
public class ApiCaller {
public final String TAG = ApiCaller.this.getClass().toString();
Context networkContext;
ApiResponseFetcher serverResponse;
//Constructor
public ApiCaller(ApiResponseFetcher response, Context ctx) {
this.serverResponse = response;
this.networkContext = ctx;
}
public void postDataToServer(int method, String url, final Map<String, String> params) {
RequestQueue queue = Volley.newRequestQueue(networkContext);
StringRequest request = new StringRequest(method, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
StopProgressBar();
System.out.println(response);
if (response != null && response.length() > 0) {
try {
serverResponse.result(response);
} catch (Exception e) {
e.printStackTrace();
StopProgressBar();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Toast.makeText(networkContext, "Error" + error.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
StopProgressBar();
Log.d("ERROR", "error => " + error.toString());
Class app_class = error.getClass();
if (app_class == AuthFailureError.class) {
Log.d("TAG", "AuthFailureError");
} else if (app_class == com.android.volley.NetworkError.class) {
Log.d("TAG", "NetworkError");
} else if (app_class == NoConnectionError.class) {
Log.d("TAG", "NoConnectionError");
} else if (app_class == com.android.volley.ServerError.class) {
Log.d("TAG", "ServerError");
} else if (app_class == com.android.volley.TimeoutError.class) {
Log.d("TAG", "TimeoutError");
} else if (app_class == ParseError.class) {
Log.d("TAG", "ParseError");
} else if (app_class == VolleyError.class) {
Log.d("TAG", "General error");
}
}
}) {
@Override
protected Map<String, String> getParams() {
return params;
}
//
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
request.setShouldCache(false);
request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
queue.add(request);
}
}
public interface ApiResponseFetcher<T> {
public void result(String resp);
Use:In Yor Activity Class
ApiCaller caller=new ApiCaller(this, context);
Map<String, String> params=new HashMap<>();
params.add("key","value");
caller.postDataToServer(Request.Method.POST,"Your_website_url",params);
Implement ApiResponseFetcher in Your Activity, It overrides result method
@Override
public void result(String resp)
{
//Parse your response here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment