You can handle the volley quick response with a custom volley class
like this: Volley class:
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.LruCache;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import static android.content.ContentValues.TAG;
public class VolleyClass {
private static VolleyClass mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private Context mCtx;
private int time = 0;
public VolleyClass(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleyClass getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleyClass(context);
}
return mInstance;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
req.setRetryPolicy(new DefaultRetryPolicy(
time,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
getRequestQueue().add(req);
}
private RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
//mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext(), new OkHttpStack(new com.squareup.okhttp.OkHttpClient()));
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
req.setRetryPolicy(new DefaultRetryPolicy(
time,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
getRequestQueue().add(req);
}
/*public ImageLoader getImageLoader() {
return mImageLoader;
}*/
}
Sync gradle with
compile 'com.android.volley:volley:1.0.0'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
Initialise the volley class:
VolleyClass volleyClass;
volleyClass = new VolleyClass(this);
Under onCreate
then call the method for operation
1.row data:
private void serverCall() {
final ProgressDialog mDialog = new ProgressDialog(SettingActivity.this);
mDialog.setMessage("Please wait...");
mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mDialog.setIndeterminate(true);
mDialog.setCancelable(false);
mDialog.show();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, "your url", null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
mDialog.dismiss();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", System.getProperty("http.agent"));
return headers;
}
@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
};
volleyClass.addToRequestQueue(jsObjRequest);
}
private void networkCallPostData() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("VolleyError " + error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("id",app.getAppSettings().__uId);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
//headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("Content-Type", "multipart/form-data");
headers.put("User-agent", System.getProperty("http.agent"));
return headers;
}
@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
};
volleyClass.addToRequestQueue(stringRequest);
}
com.android.volley.TimeoutError
and I want to handle this exception and in this exceptionnetworkResponse
comenull
. – Albarran