android - Return ArrayList from void AsyncTask -


i googled alot, no chance this. now, have inner asynctask class want use return values in upper class. , work, cause have put log.e() , shows value, here code :

   public class consume extends asynctask<void, void, void> {         private list<latlng> latlngs = new arraylist<>();         private list<contactmodel> contacts = new arraylist<>();         inputstream inputstream = null;         string result = "";          @override         protected void doinbackground(void... params) {              string url = "http://x.x.x.x/mywcf/service1.svc/json/getcontact";             arraylist<namevaluepair> param = new arraylist<namevaluepair>();             try {                 httpclient httpclient = new defaulthttpclient();                 httppost post = new httppost(url);                 post.setentity(new urlencodedformentity(param));                 httpresponse httpresponse = httpclient.execute(post);                 httpentity httpentity = httpresponse.getentity();                 inputstream = httpentity.getcontent();              } catch (unsupportedencodingexception e1) {                 log.e("unsupportedencoding", e1.tostring());                 e1.printstacktrace();             } catch (clientprotocolexception e2) {                 log.e("clientprotocolexception", e2.tostring());                 e2.printstacktrace();             } catch (illegalstateexception e3) {                 log.e("illegalstateexception", e3.tostring());                 e3.printstacktrace();             } catch (ioexception e4) {                 log.e("ioexception", e4.tostring());                 e4.printstacktrace();             }             try {                 bufferedreader breader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8);                 stringbuilder sbuilder = new stringbuilder();                  string line = null;                 while ((line = breader.readline()) != null) {                     sbuilder.append(line + "\n");                 }                 inputstream.close();                 result = sbuilder.tostring();             } catch (exception e) {                 log.e("stringbuilding", "error converting result " + e.tostring());             }             return null;         }         @override         protected void onpreexecute() {             super.onpreexecute();         }          @override         protected void onpostexecute(void avoid) {             super.onpostexecute(avoid);             list<contactmodel> contactmodels;             list<latlng> mylatlngs = new arraylist<>();             try {                  jsonobject object = new jsonobject(result);                 jsonarray jarray = object.getjsonarray("getcontactresult");                  type listtype = new typetoken<list<contactmodel>>() {                 }.gettype();                 contactmodels = new gson().fromjson(string.valueof(jarray), listtype);                 setcontacts(contactmodels);                  setcontacts(contactmodels);                 for(contactmodel contactmodel : contactmodels) {                     double latitude = double.valueof(contactmodel.getlatitude());                     double longitude = double.valueof(contactmodel.getlongitude());                     latlng latlong = new latlng(latitude, longitude);                     mylatlngs.add(latlong);                 }                 setlatlngs(mylatlngs);                 log.e("sizeofarray", mylatlngs.size()+"");             } catch (jsonexception e) {                 e.printstacktrace();             }         }      public void setcontacts(list<contactmodel> contacts) {         this.contacts = contacts;     }      public list<contactmodel> getcontacts() {         return contacts;     }      public void setlatlngs(list<latlng> latlngs) {         this.latlngs = latlngs;     }     public list<latlng> getlatlngs() {         return latlngs;     } 

and in activity class :

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.osmdroid_map);       consume consume = new consume();     consume.execute(); 

when run app, cause of line :

                log.e("sizeofarray", mylatlngs.size()+""); 

it returns me correct int number of array size in logcat, want use in app, stuff in doinbackground method, , make static variable, no chance. can 1 give real sample code? thx

as far asynctask class inner class. can directly access objects of outer class.

create object of below @ outer class level.

list<latlng> mylatlngs = new arraylist<>(); 

and remove same inner class single object instance their.

another approach use callback inner asyntask need have interface callback outer class start further process.

interface callback {     public void processingdone(); }  consume consume = new consume(new callback() {    @override    public void processingdone(list<latlng> mlatlngs) {      // stuff here    } }); consume.execute(); 

and inside consume constructor store callback pass data outer class.

class consume {   private callback mcallback;   public consume(callback callback) {      mcallback = callback;   } } 

and below link can do

if (mcallback != null) {   mcallback.processingdone(mylatlngs); } log.e("sizeofarray", mylatlngs.size()+""); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -