android - Volley only the first 3 requests in queue execute -
i making application accesses network, use service volley, 1 of things have noticed whenever add more 3 requests queue first 3 executed, use parallel approach prefer running single request per time, here application class
public class app extends application { private static final string tag = workerservice.class.getsimplename(); private static final string domain = "http://192.168.43.161"; private static final string url_get_routes = domain+"/busticket/v1/routes"; private static final string url_get_stations = domain+"/busticket/v1/stations"; private static final string url_upload_tickets = domain+"/busticket/v1/uploadtickets"; private static final string url_get_vip_cards = domain+"/busticket/v1/cards"; private static final string url_get_discounts = domain+"/busticket/v1/discounts"; private static final string url_get_customers = domain+"/busticket/v1/customers"; private database database; private arraylist<ticket> uploadtickets; public static final string tag = app.class.getsimplename(); private static app minstance; private requestqueue mrequestqueue; public static synchronized app getinstance() { return minstance; } @override public void oncreate() { super.oncreate(); utils.maketempapplicationfolder(); minstance = this; } public void setconnectivitylistener(connectivityreceiver.connectivityreceiverlistener listener) { connectivityreceiver.connectivityreceiverlistener = listener; } @override protected void attachbasecontext(context base) { super.attachbasecontext(base); } public requestqueue getrequestqueue() { if (mrequestqueue == null) { mrequestqueue = volley.newrequestqueue(getapplicationcontext()); } return mrequestqueue; } public <t> void addtorequestqueue(request<t> req, string tag) { req.settag(textutils.isempty(tag) ? tag : tag); getrequestqueue().add(req); } public <t> void addtorequestqueue(request<t> req) { req.settag(tag); getrequestqueue().add(req); } public void cancelpendingrequests(object tag) { if (mrequestqueue != null) { mrequestqueue.cancelall(tag); } }
}
and service:
public class workerservice extends service { public workerservice() { } @override public ibinder onbind(intent intent) { throw new unsupportedoperationexception("not yet implemented"); } @override public void oncreate() { super.oncreate(); app.getinstance().setconnectivitylistener(this); database = new database(getapplication()); insertdefaultschedules(); insertdefaultroutes(); downloadnewroutesandstationsanduploadtickets(); } @override public int onstartcommand(intent intent, int flags, int startid) { return start_sticky; } @override public void onnetworkconnectionchanged(boolean isconnected) { log.d(tag, "internet connectivity changed, connected? "+isconnected); } private void downloadnewroutesandstationsanduploadtickets(){ scheduledthreadpoolexecutor exec = new scheduledthreadpoolexecutor(1); exec.scheduleatfixedrate(new runnable() { public void run() { if(connectivityreceiver.isconnected()) { //my network requests, execute every after 60 mins downloadnewroutes(); downloadnewstations(); uploadsoldtickets(); deletesyncedoldticketswithnobus(); downloadnewvipcards(); downloadnewdiscounts(); downloadnewcustomers(); } } }, 0, 60, timeunit.minutes); } private void deletesyncedoldticketswithnobus(){ database.deletesyncedticketswithnobus(); } private void downloadnewroutes(){ string tag_string_req = "req_download_routes"; stringrequest strreq = new stringrequest(request.method.get, url_get_routes, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, response); try { jsonobject jobj = new jsonobject(response); jsonarray routes = jobj.getjsonarray("routes"); if (routes.length() > 0) extractandinsertroutestodatabase(routes); else insertdefaultroutes(); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override protected map<string, string> getparams() { return new hashmap<>(); } }; app.getinstance().addtorequestqueue(strreq, tag_string_req); } private void downloadnewdiscounts(){ string tag_string_req = "req_download_discounts"; stringrequest strreq = new stringrequest(request.method.get, url_get_discounts, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, "discounts: "+response); try { jsonobject jobj = new jsonobject(response); jsonarray routes = jobj.getjsonarray("discounts"); if (routes.length() > 0) extractandinsertdiscountstodatabase(routes); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override protected map<string, string> getparams() { return new hashmap<>(); } }; app.getinstance().addtorequestqueue(strreq, tag_string_req); } private void downloadnewvipcards(){ string tag_string_req = "req_download_vip_cards"; stringrequest strreq = new stringrequest(request.method.get, url_get_vip_cards, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, response); try { jsonobject jobj = new jsonobject(response); jsonarray cards = jobj.getjsonarray("cards"); if (cards.length() > 0) extractandinsertvipcardstodatabase(cards); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override protected map<string, string> getparams() { return new hashmap<>(); } }; app.getinstance().addtorequestqueue(strreq, tag_string_req); } private void downloadnewcustomers(){ string tag_string_req = "req_download_customers"; stringrequest strreq = new stringrequest(request.method.get, url_get_customers, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, "response: "+response); try { jsonobject jobj = new jsonobject(response); jsonarray customers = jobj.getjsonarray("customers"); if(customers.length() > 0) extractandinsertcustomerstodatabase(customers); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override protected map<string, string> getparams() { return new hashmap<>(); } }; app.getinstance().addtorequestqueue(strreq, tag_string_req); } private void downloadnewstations(){ string tag_string_req = "req_download_stations"; stringrequest strreq = new stringrequest(request.method.get, url_get_stations, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, "response: "+response); try { jsonobject jobj = new jsonobject(response); jsonarray schedules = jobj.getjsonarray("stations"); if(schedules.length() > 0) extractandinsertschedulestodatabase(schedules); else { insertdefaultschedules(); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override protected map<string, string> getparams() { return new hashmap<>(); } }; app.getinstance().addtorequestqueue(strreq, tag_string_req); } private void uploadsoldtickets(){ string tag_string_req = "req_uploading_tickets"; stringrequest strreq = new stringrequest(request.method.post, url_upload_tickets, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, "upload ticket response: "+response); try { jsonobject object = new jsonobject(response); if(!object.getboolean("error")) for(ticket ticket : uploadtickets) { ticket.setsynced(1); database.updateticket(ticket); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override protected map<string, string> getparams() { hashmap<string ,string> params=new hashmap<>(); params.put("tickets", getjsonarraytickets().tostring()); return params; } }; app.getinstance().addtorequestqueue(strreq, tag_string_req); }
in case
deletesyncedoldticketswithnobus(); downloadnewvipcards(); downloadnewdiscounts(); downloadnewcustomers();
methods dont run, print out response server method call log.d(tag, "response: "+response);
responses never printed out, printed out if remove other 3 methods , run them.
Comments
Post a Comment