private void doClockInOut(JSONObject params) { Response.ListenerokListener = new Response.Listener () { @Override public void onResponse(JSONObject response) { try { Log.e(service, response.getString("Status")); if ("1".equals(response.getString("Status"))) { success = true; } else if ("-4".equals(response.getString("Status"))) { View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { PersistenceUtils.getSharedInstance().put("login", "0"); MainActivity.act.startActivity(new Intent(MainActivity.act, LoginActivity.class)); MainActivity.act.finish(); } }; MessageDialog.showMessageDialogTwoButton(MainActivity.act, response.getString("Message"), "Cancel", "Login", listener); } else { error = true; //MessageDialog.showMessageDialog(MainActivity.act, response.getString("Message")); } } catch (Exception e) { e.printStackTrace(); } } }; VolleyUtils.getSharedInstance().init(MainActivity.act); VolleyUtils.getSharedInstance().requestService(MainActivity.act.getString(R.string.ws_url) + service + ".json", params, okListener, errorListener); } private void doOther(Map params) { Response.Listener okListener = new Response.Listener () { @Override public void onResponse(String response) { try { Log.e(service, response); if ("true".equals(response)) { success = true; } } catch (Exception e) { Log.e(service, e.toString()); } } }; VolleyUtils.getSharedInstance().init(MainActivity.act); VolleyUtils.getSharedInstance().requestService1(MainActivity.act.getString(R.string.ws_url) + service + ".json", params, okListener, errorListener); } Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (VolleyUtils.getSharedInstance().getMessage(error) != null) Log.e(service, VolleyUtils.getSharedInstance().getMessage(error)); } };
package com.baclock.utils;import android.app.Activity;import android.content.Context;import com.android.volley.AuthFailureError;import com.android.volley.DefaultRetryPolicy;import com.android.volley.NetworkError;import com.android.volley.NoConnectionError;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.ServerError;import com.android.volley.TimeoutError;import com.android.volley.VolleyError;import com.android.volley.toolbox.JsonObjectRequest;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.baclock.R;import org.json.JSONArray;import org.json.JSONObject;import java.util.Map;/** * Created by Jack on 1/13/2016. */public class VolleyUtils { private static VolleyUtils sharedInstance = null; private RequestQueue requestQueue = null; private String tag; private int time_out_ms = 10000; public static VolleyUtils getSharedInstance() { if (sharedInstance == null) { sharedInstance = new VolleyUtils(); } return sharedInstance; } public void init(Activity act) { if (requestQueue == null) { requestQueue = Volley.newRequestQueue(act); tag = act.getString(R.string.app_name); } } public void requestService(String url, JSONObject param, Response.ListenerOkListener, Response.ErrorListener errorListener) { JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.POST, url, param, OkListener, errorListener); jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(time_out_ms, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); jsonObjRequest.setTag(tag); requestQueue.add(jsonObjRequest); } public void JsonArrayRequestService(String url, JSONObject param, Response.Listener OkListener, Response.ErrorListener errorListener) { JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, url, param, OkListener, errorListener); request.setRetryPolicy(new DefaultRetryPolicy(time_out_ms, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); request.setTag(tag); requestQueue.add(request); } public void requestService1(String url, final Map param, Response.Listener OkListener, Response.ErrorListener errorListener) { StringRequest stringRequest = new StringRequest(Request.Method.POST, url, OkListener, errorListener) { @Override protected Map getParams() { return param; } }; stringRequest.setRetryPolicy(new DefaultRetryPolicy(time_out_ms, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); stringRequest.setTag(tag); requestQueue.add(stringRequest); } public String getMessage(VolleyError error) { if (error instanceof TimeoutError) { return "The request time out!"; } else if (isServerProblem(error)) { return "Server is error"; } else if (isNetworkProblem(error)) { return "Network is error, please check your internet!"; } else if (error.getMessage().startsWith("org.json.JSONException")) { return null; } else {// if (error != null && error.getMessage() != null)// return error.getMessage();// else// return "Carrot error"; return error.getMessage(); } } private boolean isNetworkProblem(Object error) { return (error instanceof NetworkError) || (error instanceof NoConnectionError); } private boolean isServerProblem(Object error) { return (error instanceof ServerError) || (error instanceof AuthFailureError); }}