thread safety - Android - result from AsyncTask not being returned to main Activity -


i'm trying use asynctask-extended class handle connecting url, parsing json, displaying indeterminate progressdialog during parsing, , returning results key-value pairs in hashmap main activity. results of hashmap read main activity , put form fields. however, though i'm populating hashmap in asynctask (evidenced println statements), calling method in main activity returns hashmap yields empty result. can't figure out if i'm doing wrong, or if i'm misunderstanding capabilities of asynctask.

i'm debating converting class extends asynctask activity. essentially, user should not able else during data search/parsing , should wait until progressdialog goes away before can interact application again (or hitting button). also, application needs able handle cases in asynctask exceptions caught (can't connect url, bad json, product id search cannot found) , custom error dialogs tailored exceptions. if class activity, send different result codes when calling finish(), depending on if exception caught.

again, i'm not sure if asynctask best solution here, since user not doing else while information being gathered , parsed. please advise me if new activity make sense or if i'm mangling implementation of background thread.

mainactivity.java

minitiateproductlookupbutton.setonclicklistener(new view.onclicklistener() {             public void onclick(view v) {                 productlookup pl = new productlookup(id, mainactivity.this);                 pl.execute();                  // below variable empty!                 hashmap<string, string> productinfo = pl.getproductinfo();                 applyproductinfotoformfields(productinfo);             }         }); 

productlookup.java

public class productlookup extends asynctask<object, void, hashmap<string, string>> {     private string mproductid;     private context mcontext;     hashmap<string, string> mproductinfo;     progressdialog mdialog;      public productlookup(string id, context applicationcontext) {         mproductid = id;         mcontext = applicationcontext;         mproductinfo = new hashmap<string, string>();     }      @override     protected void onpreexecute() {         mdialog = new progressdialog(mcontext);         mdialog.setmessage("loading product info. please wait...");         mdialog.setindeterminate(true);         mdialog.setcancelable(false);         mdialog.show();     }      @override     protected void onpostexecute(hashmap<string, string> result){        super.onpostexecute(result);        mdialog.dismiss();        mproductinfo = result;     }       @override     protected hashmap<string, string> doinbackground(object... params) {         try {             // connect url, parse json, , add key-value pairs mproductinfo...          } catch (malformedurlexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } catch (jsonexception e) {             e.printstacktrace();         }         {             try {                 // close input/output reader variables             } catch (ioexception e) {                 e.printstacktrace();             }         }         return mproductinfo;      }      public hashmap<string, string> getproductinfo(){         return this.mproductinfo;     }  } 

when issue .execute() runs threaded , doesn't wait result.

so whatever call after it, empty data has not been loaded yet.

you need set on postexecuted result directly activity via setter mainactivity.this.setproductinfo(result)


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -