Created
August 28, 2017 11:43
-
-
Save saiumesh535/8cb466d1036a8c247d629b3a3d9514f2 to your computer and use it in GitHub Desktop.
Volley Server request class
This file contains 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
/** | |
* this class helps us to make http calls | |
*/ | |
public class HttpCaller { | |
private Context context; | |
//private ProgressDialog pDialog; | |
private HttpProgressDialog httpProgressDialog; | |
public HttpCaller(Context context) { | |
this.context = context; | |
} | |
// this is for POST method | |
public void callServer(String URL, | |
final HashMap<String, String> params, | |
final HttpCaller.VolleyCallback callback) { | |
if (!checkInternetConnection(context)) { | |
showNoInternetDialogue(context); | |
return; | |
} | |
httpProgressDialog = new HttpProgressDialog(context); | |
httpProgressDialog.showDialog(); | |
RequestQueue requestQueue = Volley.newRequestQueue(context); | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
callback.onSuccess(response); | |
httpProgressDialog.hideDialog(); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError volleyError) { | |
callback.onError(volleyError.toString()); | |
showSMR(context, volleyError); | |
httpProgressDialog.hideDialog(); | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() throws AuthFailureError{ | |
Log.e("serverParams", params.toString()); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders() { | |
return new HashMap<>(); | |
} | |
}; | |
//setting up the retry policy for slower connections | |
int socketTimeout = 120000;//120000 milli seconds - change to what you want | |
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); | |
stringRequest.setRetryPolicy(policy); | |
requestQueue.add(stringRequest); | |
} | |
public void getCaller(String URL, final HttpCaller.VolleyCallback callback) { | |
if (!checkInternetConnection(context)) { | |
showNoInternetDialogue(context); | |
return; | |
} | |
httpProgressDialog = new HttpProgressDialog(context); | |
httpProgressDialog.showDialog(); | |
RequestQueue requestQueue = Volley.newRequestQueue(context); | |
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
callback.onSuccess(response); | |
httpProgressDialog.hideDialog(); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError volleyError) { | |
callback.onError(volleyError.toString()); | |
showSMR(context, volleyError); | |
httpProgressDialog.hideDialog(); | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() { | |
return new HashMap<>(); | |
} | |
@Override | |
public Map<String, String> getHeaders() { | |
Map<String, String> headers = new HashMap<>(); | |
headers.put("Content-Type", "application/x-www-form-urlencoded"); | |
return headers; | |
} | |
}; | |
//setting up the retry policy for slower connections | |
int socketTimeout = 120000;//120000 milli seconds - change to what you want | |
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); | |
stringRequest.setRetryPolicy(policy); | |
requestQueue.add(stringRequest); | |
} | |
// interface for callback | |
public interface VolleyCallback { | |
void onSuccess(String result); | |
void onError(String error); | |
} | |
private static boolean checkInternetConnection(Context context) { | |
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | |
return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); | |
} | |
private static void showNoInternetDialogue(final Context context) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(context); | |
builder.setCancelable(true); | |
builder.setMessage("Please enable Internet"); | |
builder.setPositiveButton("WIFI SETTINGS", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
context.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); | |
} | |
}); | |
builder.setNegativeButton("MOBILE INTERNET", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); | |
} | |
}); | |
builder.setTitle(R.string.app_name); | |
AlertDialog dialog = builder.create(); | |
dialog.show(); | |
} | |
} |
This file contains 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
/** | |
* this is for showing progress dialogue when user makes http call | |
*/ | |
public class HttpProgressDialog { | |
private Dialog dialog; | |
private ProgressBar progressBar; | |
public HttpProgressDialog(Context context) { | |
dialog = new Dialog(context); | |
dialog.setContentView(R.layout.progress_layout); | |
dialog.setCancelable(false); | |
if(dialog.getWindow() != null){ | |
dialog.getWindow() | |
.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, | |
ViewGroup.LayoutParams.WRAP_CONTENT); | |
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); | |
} | |
progressBar = (ProgressBar)dialog.findViewById(R.id.progressBar); | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
progressBar.getIndeterminateDrawable().setColorFilter(randomColor(), PorterDuff.Mode.MULTIPLY); | |
new Handler().postDelayed(this, 300); | |
} | |
}, 0); | |
} | |
public void showDialog(){ | |
dialog.show(); | |
} | |
public void hideDialog(){ | |
dialog.cancel(); | |
} | |
// get random color | |
private int randomColor(){ | |
int[] colors = new int[]{ | |
Color.RED, | |
Color.YELLOW, | |
Color.WHITE, | |
Color.GREEN, | |
Color.BLUE | |
}; | |
return colors[new Random().nextInt(4)]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment