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.
Related
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 .
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.
I am trying to get the response from instagram api using volley, but can't get the data. I did't receive any call back methods like onResponse or onErrorResponse. Nothing show up. I Could not see any error.
here is my code.
public String getUserId(String usrName) {
url = TContants.urlBeforeUserId + usrName + TContants.urlAfterUser;
JsonObjectRequest jsonObjReq;
jsonObjReq = new JsonObjectRequest(Method.GET, url,
null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
TagsResponse gsonData = gson.fromJson(response.toString(), TagsResponse.class);
userId = gsonData.data[0].id.toString();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley:", "getUserId response error");
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq,
TContants.tag_json_obj);
return userId;
}
url is working fine, I tested it.
When code running JsonObjectRequest creating. but next step it skip the onResponse and onErrorResponse methods. plz help.
The code inside onResponse is not skipped, that's how it's supposed to work, what you are looking at is a Callback.
A very quick and general explanation would be:
this code does not run serially, instead, onResponse in this case is your implementation of an interface provided by the request object, that will be called when the response arrives, this might take several milliseconds to seconds (depending on the server, since this callback is for a network operation).
Read about callback handling (both network, and the simple ones you create with interfaces - and if you haven't yet - read about interfaces), as it is a major part of programming.
ADDITION:
To see when the response does return, I would print all the parameters before sending them to make sure they are sent correctly, and also print the response itself (response.toString() at the beginning of onResponse) and wait a bit to see it after the request is sent.
(don't be alarmed if the print won't contain readable info, it depends on the implementation of the .toString() method, for now it's just an indication that you got a response at all).
I want to send two different requests and handle two different responses in one Activity using Volley library.
My activity implements onResponseListener, so i have only one onResponse method and both responses are handled here. As they are completely same in structure i cant tell which is which.
How can i tell from which request i have received the response so i can handle them differently? Is there a way to "tag" a request or something like that?
I could set some kind of check variable, e.g. boolean firstRequestIsSent when i send the request, and then check it in the onResponse method, but its a pretty ugly solution.
Many thanks
Instead of implementing onResponse as part of the class, you can instantiate a new Response.Listener with the request. This way you will have a separate listener for each request.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener() {
#Override
public void onResponse(String response) {
// individual response here
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// error here
}
});
I've seen answer to both of these questions, however, when I tried to put them together, I couldn't make it work. The problem itself is pretty simple: I want to get a string from one site and use it in a post request. That means I can only make the post request after I've finished parsing the GET request. The main ideas I'm using are these ones:
How to return response header field to main method using Google Volley for HTTP GET request in Android / Java?
Can I do a synchronous request with volley?
However the synchronous request is blocked and doesn't go on, and the first one is Async.
I believe this to be a simple thing to do, but still, I haven't be able to do it...
Thanks for any help!
Why not do something like this:
// send first request
requestQueue.add(firstRequest, null, new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// ** code to parse response **
// send second request
requestQueue.add(secondRequest, null, new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// ** code to parse response **
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// ** code to handle errors **
}
}));
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// ** code to handle errors **
}
}));