android - How to return value from helper class with AsyncTask -


i trying design helper class implements methods using asynctask.

public interface resultcallback {     public string processresult(); }  public class serveradapter {      // required processresult call method. kind of lousy not know     // how throw exception onpostexcecute in asynctask.     public string getresult() throws airplanemodeexception, nonetworkexception     {        // code return value dowork throw exceptions on errors     }      public void getlicense(resultcallback licensecallback)     {         ...// set url, outmessage         new dowork(url, outmessage, licensecallback).execute();     }      public void queryserver(int queryid, arraylist<string> args, resultcallback querycallback)     {         ...// set url, outmessage         new dowork(url, outmessage, querycallback);     }      private class dowork extends asynctask<void, void, string>     {         ...         private resultcallback rc;          public dowork(string url, string outmessage, resultcallback rc)         {             // code here         }          protected string doinbackground(void... params)         {             try             {                  // code here             }             catch (airplanemodeexception e)             {                 return "airplanemodeexception";             }             catch ...          }           protected void onpostexecute(string result)          {              this.result = result;              cb.processresult();          }     } }  // client class public class myclass extends activity {     myserveradapter myadapter;      public void oncreate(bundle savedinstancestate)     {         ...         myadapter = new serveradapter();         myadapter.getlicence(new mylicensecallback);         myadapter.queryserver(id, args, new myquerycallback);         ...     }      public class mylicensecallback extends resultcallback implements processresult     {         try         {             string result = myadapter.getresult;             ...         }         catch         ...      }     ...  } 

i new java , android , have couple of questions:

1- several serveradapter method calls cause synchronize problem? example while code mylicense callback running, if onpostexecute calls myquerycallback, have handle or java handles it?

2- how exception thrown in dowork thrown in callback instead of work around in code above?

  1. android guarantees methods in activity , asynctask.onpostexecute runs in same main ui thread.

  2. you save exception in task instance variable same way result (return, null result in case). check if exception present or not later handle error situation.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -