Volley framewok request keeps objects in memory - android

I'm making a volley request in this way:
public void makeRequest(BaseRequest request, Response.Listener<JSONObject> responseListener,
Response.ErrorListener errorListener) {
if (Constants.DEBUG) Log.i(TAG, "Sending: " + request.getUrlRequest());
JsonObjectRequest jsObjRequest = new JsonObjectRequest(METHOD, request.getUrlRequest(), null, responseListener, errorListener);
// disable cache
jsObjRequest.setShouldCache(false);
jsObjRequest.setTag(mTag);
mQueue.add(jsObjRequest);
}
mTag is a Class type. I have an activity where on its onCreate method I call the volley request with this:
mVolleyManager.makeRequest(getRequest(), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
refreshLayout.setRefreshing(false);
onEndLoading(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
refreshLayout.setRefreshing(false);
onErrorLoading(error);
}
});
If I start open and closing the activity for a bunch of time, my memory keeps growing till it reaches an OOM error. I tried to have a look with MAT and here's the result:
It seems that Volley keeps in memory all of its requests, even if the onResponse method is correctly called. I already solved the problem by switching to Retrofit. Same code, same requests and it's working but I want to understand what could be the cause of my problem.

NetworkDispatcher has only 4 threads by default so i think your problem is caused by creating multiple requestqueue and old threads dose not killed, use singleton queue, for refering to that look at:
http://developer.android.com/training/volley/requestqueue.html
each request queue has only 4 threads but in your image i can see more than 4 so obviously your problem caused by not using singleton design pattern.

Related

Android Volley callback after multiple request completes

I have made a splash screen where im syncing data from server to sqlite. I have to make few requests (lets say 5 request for 5 tables), and then the splash screen goes off and main activity starts. Please share some ideas as Im clueless how to achieve this. I have left no stone unturned in stackoverflow but could not find a perfect workaround.
Add Volley https://stackoverflow.com/questions/16659620/volley-android-networking-library?rq=1
Then you can make calls from volley
JsonObjectRequest jsonObjReq = new JsonObjectRequest(com.android.volley.Request.Method.POST, Url__, ipObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
listener.resultJson(null, response);
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
listener.resultJson(null, response); will get the responce from server and save this responce to your Sqlite.Finally close your Spalsh .

Volley JSONObjectRequest returns nothing

I want to use Volley to get a JSON response from some website, so I started testing it. Here is my code plain and simple:
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, "http://api.androidhive.info/volley/person_object.json", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
json = response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
error.printStackTrace();
}
});
int x = json.length();
After the request is made the response is always null. Neither an error is raised neither the request succeeds. Which is really confusing. As you can see I am assigning the value of the response to a variable named json which is of the same type. When I debug the application by putting a breakpoint on the onResponse method, onErrorResponse method and on the last line, the debugger only hits the last line the variables watches indicate that the value of the response is null.
I have tried more than one URL
http://simplifiedcoding.16mb.com/UserRegistration/json.php
https://androidtutorialpoint.com/api/volleyString
I have added Volley via gradle
compile 'com.android.volley:volley:1.0.0'
Put it in a RequestQueue
RequestQueue queue = Volley.newRequestQueue(this);
Then
queue.add(jsObjRequest);
Or
ApplicationController.getInstance().addToRequestQueue(jsObjRequest);
You need to add the request to the queue for the asynchronous request to work.
RequestQueue requestQueue= Volley.newRequestQueue(this)
requestQueue.add(jsObjRequest);

How to add a specific time for Volley Requesting

I am using multiple volley request in TabLayout, Tab1(1st req) and Tab2(2nd req).Problem is both requesting at the same time,which made App froze during swapping in TabLayout from Tab1 to Tab2.I want a specific delay,requesting the second.I hope you understood the scenario,feel free to ask more suggestions/clarification.
I highly doubt that the app is freezing due to sending a request through volley.
Anyway if want to send the second request after the first request, put it in the response listener of first request like
JsonObjectRequest firstRequest = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//Send 2nd request here
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Handle Error case here
}
});
mRequestQueue.add(firstRequest);
If you want to send the second request after x seconds after switching to the tab 2, you can use postDelayed where you can detect the tab is changed (e.g., onResume if you are using fragments as tabs) like
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
//Your second request goes here
}
},
1000); //Will execute after one second.
For some advanced usages you can also configure request queue size of volley. For your specific scenario you can set number of maximum parallel request to one as described here.

What is the best way to coordinate Volley requests

I'm trying to use Volley to execute multiple HTTP request where each one of them relies on the result of the previous one, what is the best option as a design?
1-Firing the next request in the onResponse callback of the previous request?
2-Writing some coordinator class that have callbacks that get called in the onResponse method of a request and fires the next request
skeleton code for the second option
coodrinator = new Coordinator();
JsonObjectRequest firstRequest = new JSONObjectRequest(Request.Method.GET,firstURL),new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//handle the responsee
coordinator.onFirstRequestRecieved();
}
},
errorListener);
private void doSecondRequest(){
JsonObjectRequest secondRequest = new JSONObjectRequest(Request.Method.GET,secondURL),new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//handle the responsee
coordinator.onSecondRequestRecieved();
}
},
errorListener);
}
private class Coordinator{
public void onFirstReequestRecieved(){
doSecondRequest();
}
public void onSecondRequestRecieved(){
//do Something
}
}
If first request response parameters are needed for making second request and so on then you can go for synchronous way. That can be achieved by making second request in onResponse on First request can there is no good or bad practise for it.
The thing is volley is asynchronous and request what is added in the queue execute without depending on other request and we are going to make it synchronous request and it can be achieved by many ways seeing your requirement.

Android Volley store requests when offline

I've searched all over but haven't found an answer to this.
In my Android application, the user can use the app offline, and some events generate http GET requests to my server. I am using Volley, and when the user is online the requests work properly, but offline they fail immediately and are removed from the request queue.
I wanted Volley to store these requests, and try again when the server is accessible, or at least keep trying. Is this behaviour possible?
Here's how I'm doing things:
StringRequest request = new StringRequest(Request.Method.GET, url, listener, listener);
request.setRetryPolicy(new DefaultRetryPolicy(8000, 2, 1.5f));
postToDefaultQueue(request);
private void postToDefaultQueue (StringRequest request) {
if (sQueue == null) {
sQueue = Volley.newRequestQueue(mContext.get());
}
sQueue.add(request);
}
Thanks so much, any help appreciated
Edit
So I managed to make the request go back to the queue after it fails. The code is below:
private class DummyListener implements Response.Listener<String>, Response.ErrorListener {
String mUrl;
public DummyListener (String url){
mUrl = url;
}
#Override
public void onErrorResponse(final VolleyError error) {
new Timer().schedule(new TimerTask() {
#Override
public void run() {
Log.v(DummyListener.class.getSimpleName(), "ErrorResponse", error);
offlineStringGetRequest(mUrl);
}
}, 5000);
}
#Override
public void onResponse(String response) {
Log.d(DummyListener.class.getSimpleName(), "Response: " + response);
}
}
The problem that remains is that if I kill the app, the queue gets destroyed, and the request is never made.
Edit2
Noticed some people liked this question. I ended up using Path (https://github.com/path/android-priority-jobqueue) with some modifications for the job. It worked perfectly. Just wish Volley had a native way for doing this
Since I found no way to do this with Volley, I ended up using Path
And it worked wonders

Categories

Resources