I am working on an Android Application that is entirely dependent on RESTful APIs. Almost every screen of Application is making a network call. I am using following helper class for making network requests.
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
public class RequestHandler {
private final String deviceID;
private RequestQueue queue;
private StringRequest stringRequest;
private NetworkRequestListener networkRequestListener;
private NetworkResponseListener networkResponseListener;
private SharedPreferences sharedPreferences;
public RequestHandler(Context context) {
queue = Volley.newRequestQueue(context);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
deviceID = UtilityMethods.getDeviceId(context);
}
public void makeNetworkRequest(String url, int method, final String body) {
stringRequest = new StringRequest(method, appendedURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("RESPONSE", response);
if (networkResponseListener != null) {
networkResponseListener.onResponseLoaded(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (networkResponseListener != null) {
networkResponseListener.onResponseFailed(error);
}
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() {
try {
return body.getBytes("utf-8");
} catch (UnsupportedEncodingException | NullPointerException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", body, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("version", AppConstants.VERSION);
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse networkResponse) {
String response = UtilityMethods.networkResponseToString(networkResponse);
networkResponseListener.onResponseReceived(response);
return super.parseNetworkResponse(networkResponse);
}
};
stringRequest.setShouldCache(false);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
if (networkRequestListener != null) {
networkRequestListener.beforeMakeRequest(stringRequest);
}
queue.add(stringRequest);
}
public void setNetworkResponseListener(NetworkResponseListener networkResponseListener) {
this.networkResponseListener = networkResponseListener;
}
public void setNetworkRequestListener(NetworkRequestListener networkRequestListener) {
this.networkRequestListener = networkRequestListener;
}
public RequestQueue getQueue() {
return queue;
}
public StringRequest getStringRequest() {
return stringRequest;
}
}
Now issue with this is, that I am not aware of a method to cancel network request (Setting callback interfaces to null isn't working as well). If the response from network call comes after activity being destroyed, the handler methods throw a NPE because of either context being null or a view (Though activity is destroyed the code is still executed). Is there a possible way to cancel/ignore a network response when activity is destroyed? Also If this implementation could be improved as almost every activity is having a memory leak due to this class.
Create a method in this class like this
public static final String TAG = "MyTag";
StringRequest stringRequest; // Assume this exists.
RequestQueue queue; // Assume this exists.
// Set the tag on the request.
stringRequest.setTag(TAG);
// Add the request to the RequestQueue.
queue.add(stringRequest);
public void cancelTag() {
return queue.cancelAll(TAG);;
}
In the Activity onStop()
call this CancelTag()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.