Friday 15 March 2013

java - ArrayList returning null value -



java - ArrayList returning null value -

i'm working on java application. create singleton class restrict instantiation of class 1 object. in same class i've got method returns arraylist of object called guestagent. here method:

//singleton class: tenant public arraylist<guestagent> gagentlist() { final arraylist<guestagent> guestagents = new arraylist<>(); string url = "http://localhost:8080/stackui/v2.0/"; url = url + this.tenantid; url = url + "/os-agents"; requestbuilder builder = new requestbuilder(requestbuilder.get, url.encode(url)); builder.setheader("x-auth-token", this.tokenid); seek { builder.sendrequest(null, new requestcallback() { @override public void onerror(request request, throwable exception) { window.alert("attenzione si è verificato united nations errore"); } @override public void onresponsereceived(request request, response response) { if (200 == response.getstatuscode()) { final html respbox = new html(); respbox.sethtml(response.gettext()); string risposta = response.gettext(); jsonvalue jsonvalue; jsonarray jsonarray; jsonobject jsonobject; jsonstring jsonstring; jsonnumber jsonnumber; jsonvalue = jsonparser.parsestrict(risposta); if ((jsonobject = jsonvalue.isobject()) == null) { window.alert("error parsing json"); } jsonvalue = jsonobject.get("agents"); if ((jsonarray = jsonvalue.isarray()) == null) { window.alert("error parsing json"); } (int = 0; < jsonarray.size(); i++) { guestagent guestagent = new guestagent(); jsonvalue = jsonarray.get(i); if ((jsonobject = jsonvalue.isobject()) == null) { window.alert("error parsing json"); } jsonvalue = jsonobject.get("agent_id"); if ((jsonnumber = jsonvalue.isnumber()) == null) { window.alert("error parsing json"); } guestagent.setagentid(jsonnumber.tostring()); jsonvalue = jsonobject.get("architecture"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setarchitecture(jsonstring.stringvalue()); jsonvalue = jsonobject.get("hypervisor"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.sethypervisor(jsonstring.stringvalue()); jsonvalue = jsonobject.get("md5hash"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setmd5hash(jsonstring.stringvalue()); jsonvalue = jsonobject.get("os"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setos(jsonstring.stringvalue()); jsonvalue = jsonobject.get("url"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.seturl(jsonstring.stringvalue()); jsonvalue = jsonobject.get("version"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setversion(jsonstring.stringvalue()); guestagents.add(guestagent); } } else { // handle error. can status text response.getstatustext() window.alert("errore " + response.getstatuscode() + " " + response.getstatustext()); } } }); } grab (requestexception e) { // couldn't connect server window.alert("impossibile connettersi al server"); } homecoming guestagents; }

method activation other class:

//other class arraylist<guestagent> agents; agents = tenant.gettenantobject().gagentlist(); window.alert(integer.tostring(agents.size()));

at point, i've found out agents list empty. hope help. giacomo.

the phone call made requestbuilder asyncronous, meaning that, after calling builder.sendrequest, takes time run 1 of 2 callbacks methods onerror , onresponsereceived.

your problem correctly start async process, returning guestagents array immediately! (look @ lastly line of code). @ point result of async phone call not yet ready , array still empty.

methods don't provide homecoming value, take callback function argument called when process finishes , contain resulting values. in other words, need wait request completed before accessing guestagents array.

i way (i did simple notepad without compiling, there errors...):

//other class arraylist<guestagent> agents; agents = tenant.gettenantobject().gagentlist(new agentsresultcallback { void oncompleted(arraylist<guestagent> agents) { // here have result! if (agents != null) { // check errors window.alert(integer.tostring(agents.size())); } } });

the singleton:

//singleton class: tenant (look @ void homecoming value!) public void gagentlist(final agentsresultcallback callback) { final arraylist<guestagent> guestagents = new arraylist<>(); string url = "http://localhost:8080/stackui/v2.0/"; url = url + this.tenantid; url = url + "/os-agents"; requestbuilder builder = new requestbuilder(requestbuilder.get, url.encode(url)); builder.setheader("x-auth-token", this.tokenid); seek { builder.sendrequest(null, new requestcallback() { @override public void onerror(request request, throwable exception) { window.alert("attensione si è verificato united nations errore"); callback.oncompleted(null); // phone call callback null results } @override public void onresponsereceived(request request, response response) { if (200 == response.getstatuscode()) { final html respbox = new html(); respbox.sethtml(response.gettext()); string risposta = response.gettext(); jsonvalue jsonvalue; jsonarray jsonarray; jsonobject jsonobject; jsonstring jsonstring; jsonnumber jsonnumber; jsonvalue = jsonparser.parsestrict(risposta); if ((jsonobject = jsonvalue.isobject()) == null) { window.alert("error parsing json"); } jsonvalue = jsonobject.get("agents"); if ((jsonarray = jsonvalue.isarray()) == null) { window.alert("error parsing json"); } (int = 0; < jsonarray.size(); i++) { guestagent guestagent = new guestagent(); jsonvalue = jsonarray.get(i); if ((jsonobject = jsonvalue.isobject()) == null) { window.alert("error parsing json"); } jsonvalue = jsonobject.get("agent_id"); if ((jsonnumber = jsonvalue.isnumber()) == null) { window.alert("error parsing json"); } guestagent.setagentid(jsonnumber.tostring()); jsonvalue = jsonobject.get("architecture"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setarchitecture(jsonstring.stringvalue()); jsonvalue = jsonobject.get("hypervisor"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.sethypervisor(jsonstring.stringvalue()); jsonvalue = jsonobject.get("md5hash"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setmd5hash(jsonstring.stringvalue()); jsonvalue = jsonobject.get("os"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setos(jsonstring.stringvalue()); jsonvalue = jsonobject.get("url"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.seturl(jsonstring.stringvalue()); jsonvalue = jsonobject.get("version"); if ((jsonstring = jsonvalue.isstring()) == null) { window.alert("error parsing json"); } guestagent.setversion(jsonstring.stringvalue()); guestagents.add(guestagent); } // finished! results finish send them callback callback.oncompleted(guestagents); } else { // handle error. can status text response.getstatustext() window.alert("errore " + response.getstatuscode() + " " + response.getstatustext()); callback.oncompleted(null); // phone call callback null results here, } } }); } grab (requestexception e) { // couldn't connect server window.alert("impossibile connettersi al server"); } return; // homecoming nothing! }

and little declaration of callback class:

abstract public class agentsresultcallback { abstract void oncompleted(arraylist<guestagent> agents); }

java android json arraylist singleton

No comments:

Post a Comment