How one interface can be used for more than two background android tasks within one activity?How can I concatenate two arrays in Java?How do save an Android Activity state using save instance state?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?Is AsyncTask really conceptually flawed or am I just missing something?Android - Cancel AsyncTask ForcefullyAndroid - Start a new activity while the application is in the backgroundHow to differentiate two data sets returned by the same custom interface methodWhy AsyncTask is not working?

On The Origin of Dissonant Chords

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

a sore throat vs a strep throat vs strep throat

can anyone help me with this awful query plan?

How could Tony Stark make this in Endgame?

Minor Revision with suggestion of an alternative proof by reviewer

Pulling the rope with one hand is as heavy as with two hands?

Are there physical dangers to preparing a prepared piano?

Implications of cigar-shaped bodies having rings?

Two field separators (colon and space) in awk

Read line from file and process something

"You've called the wrong number" or "You called the wrong number"

How to fry ground beef so it is well-browned

Is it idiomatic to construct against `this`

Could the terminal length of components like resistors be reduced?

How much cash can I safely carry into the USA and avoid civil forfeiture?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

What's the polite way to say "I need to urinate"?

As an international instructor, should I openly talk about my accent?

How to denote matrix elements succinctly?

Multiple options vs single option UI

How to write a column outside the braces in a matrix?

Is Diceware more secure than a long passphrase?

Map of water taps to fill bottles



How one interface can be used for more than two background android tasks within one activity?


How can I concatenate two arrays in Java?How do save an Android Activity state using save instance state?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?Is AsyncTask really conceptually flawed or am I just missing something?Android - Cancel AsyncTask ForcefullyAndroid - Start a new activity while the application is in the backgroundHow to differentiate two data sets returned by the same custom interface methodWhy AsyncTask is not working?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have an activity class which is calling background AsyncTask twice like



BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
bt.execute(url1);

BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
bt.execute(url2);


one is used to GET the data and another is used to POST the data to server.



Constructors of this AsyncTask is as follows



public BackgroundTask(Context context, String method) 
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;


public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
this.params = params;



Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as



public interface onBackgroundTaskListener<T> 
void onTaskComplete(T result);



onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.



@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
callback.onTaskComplete(result);



Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...



@Override
public void onTaskComplete(String result)
if (execution == 1)
// Parse the GET result
else if(execution == 2)
// Parse the POST result




I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.










share|improve this question




























    1















    I have an activity class which is calling background AsyncTask twice like



    BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
    bt.execute(url1);

    BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
    bt.execute(url2);


    one is used to GET the data and another is used to POST the data to server.



    Constructors of this AsyncTask is as follows



    public BackgroundTask(Context context, String method) 
    this.context = context;
    this.method = method;
    this.callback = (onBackgroundTaskListener<String>) context;


    public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
    this.context = context;
    this.method = method;
    this.callback = (onBackgroundTaskListener<String>) context;
    this.params = params;



    Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as



    public interface onBackgroundTaskListener<T> 
    void onTaskComplete(T result);



    onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.



    @Override
    protected void onPostExecute(String result)
    super.onPostExecute(result);
    callback.onTaskComplete(result);



    Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...



    @Override
    public void onTaskComplete(String result)
    if (execution == 1)
    // Parse the GET result
    else if(execution == 2)
    // Parse the POST result




    I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.










    share|improve this question
























      1












      1








      1








      I have an activity class which is calling background AsyncTask twice like



      BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
      bt.execute(url1);

      BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
      bt.execute(url2);


      one is used to GET the data and another is used to POST the data to server.



      Constructors of this AsyncTask is as follows



      public BackgroundTask(Context context, String method) 
      this.context = context;
      this.method = method;
      this.callback = (onBackgroundTaskListener<String>) context;


      public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
      this.context = context;
      this.method = method;
      this.callback = (onBackgroundTaskListener<String>) context;
      this.params = params;



      Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as



      public interface onBackgroundTaskListener<T> 
      void onTaskComplete(T result);



      onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.



      @Override
      protected void onPostExecute(String result)
      super.onPostExecute(result);
      callback.onTaskComplete(result);



      Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...



      @Override
      public void onTaskComplete(String result)
      if (execution == 1)
      // Parse the GET result
      else if(execution == 2)
      // Parse the POST result




      I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.










      share|improve this question














      I have an activity class which is calling background AsyncTask twice like



      BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
      bt.execute(url1);

      BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
      bt.execute(url2);


      one is used to GET the data and another is used to POST the data to server.



      Constructors of this AsyncTask is as follows



      public BackgroundTask(Context context, String method) 
      this.context = context;
      this.method = method;
      this.callback = (onBackgroundTaskListener<String>) context;


      public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
      this.context = context;
      this.method = method;
      this.callback = (onBackgroundTaskListener<String>) context;
      this.params = params;



      Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as



      public interface onBackgroundTaskListener<T> 
      void onTaskComplete(T result);



      onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.



      @Override
      protected void onPostExecute(String result)
      super.onPostExecute(result);
      callback.onTaskComplete(result);



      Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...



      @Override
      public void onTaskComplete(String result)
      if (execution == 1)
      // Parse the GET result
      else if(execution == 2)
      // Parse the POST result




      I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.







      java android android-asynctask






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 17:28









      rpurantrpurant

      100113




      100113






















          2 Answers
          2






          active

          oldest

          votes


















          0














          enum BackgroundType GET, POST 

          public interface onBackgroundTaskListener<T>
          void onTaskComplete(T result, BackgroundType type);


          --------------------------
          BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
          bt.execute(url1);

          BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
          bt.execute(url2);
          ------------------------
          @Override
          public void onTaskComplete(String result, BackgroundType type)
          switch(type)
          case GET:
          break;
          case POST:
          break;







          share|improve this answer






























            0














            I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask



            Then the new class can then encapsulate the string you require along with other variables you would need.



            For example:



            // Create a new class to hold the result of `AsyncTask` execution
            public class TaskExecutionResult
            public String method;
            public String resultString;


            // When subclassing AsyncType assign TaskExecutionResult as return type
            // Please replace URL with input parameters you require (perhaps a method string)
            private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>

            // doInBackground will now return TaskExecutionResult type of object
            protected TaskExecutionResult doInBackground(URL... urls)
            // Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
            ...


            // onPostExecute will also accept TaskExecutionResult type of object
            protected void onPostExecute(TaskExecutionResult result)
            super.onPostExecute(result);
            callback.onTaskComplete(result);




            Then use TaskExecutionResult in onTaskComplete:



            public void onTaskComplete(TaskExecutionResult result) 
            if (result.method.equals("GET"))
            // Parse the GET result (from result.resultString)
            else if(result.method.equals("POST"))
            // Parse the POST result (from result.resultString)




            This approach will also allow you to easily update the result object if you needed to add more return variables.






            share|improve this answer

























            • could you please elaborate more. Could not understand what you want say.

              – rpurant
              Mar 23 at 5:45











            • @rpurant, I've added an example. Hope it helps

              – foobar
              Mar 24 at 10:44











            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304926%2fhow-one-interface-can-be-used-for-more-than-two-background-android-tasks-within%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            enum BackgroundType GET, POST 

            public interface onBackgroundTaskListener<T>
            void onTaskComplete(T result, BackgroundType type);


            --------------------------
            BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
            bt.execute(url1);

            BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
            bt.execute(url2);
            ------------------------
            @Override
            public void onTaskComplete(String result, BackgroundType type)
            switch(type)
            case GET:
            break;
            case POST:
            break;







            share|improve this answer



























              0














              enum BackgroundType GET, POST 

              public interface onBackgroundTaskListener<T>
              void onTaskComplete(T result, BackgroundType type);


              --------------------------
              BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
              bt.execute(url1);

              BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
              bt.execute(url2);
              ------------------------
              @Override
              public void onTaskComplete(String result, BackgroundType type)
              switch(type)
              case GET:
              break;
              case POST:
              break;







              share|improve this answer

























                0












                0








                0







                enum BackgroundType GET, POST 

                public interface onBackgroundTaskListener<T>
                void onTaskComplete(T result, BackgroundType type);


                --------------------------
                BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
                bt.execute(url1);

                BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
                bt.execute(url2);
                ------------------------
                @Override
                public void onTaskComplete(String result, BackgroundType type)
                switch(type)
                case GET:
                break;
                case POST:
                break;







                share|improve this answer













                enum BackgroundType GET, POST 

                public interface onBackgroundTaskListener<T>
                void onTaskComplete(T result, BackgroundType type);


                --------------------------
                BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
                bt.execute(url1);

                BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
                bt.execute(url2);
                ------------------------
                @Override
                public void onTaskComplete(String result, BackgroundType type)
                switch(type)
                case GET:
                break;
                case POST:
                break;








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 22 at 17:38









                BahmanBahman

                1,3012717




                1,3012717























                    0














                    I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask



                    Then the new class can then encapsulate the string you require along with other variables you would need.



                    For example:



                    // Create a new class to hold the result of `AsyncTask` execution
                    public class TaskExecutionResult
                    public String method;
                    public String resultString;


                    // When subclassing AsyncType assign TaskExecutionResult as return type
                    // Please replace URL with input parameters you require (perhaps a method string)
                    private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>

                    // doInBackground will now return TaskExecutionResult type of object
                    protected TaskExecutionResult doInBackground(URL... urls)
                    // Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
                    ...


                    // onPostExecute will also accept TaskExecutionResult type of object
                    protected void onPostExecute(TaskExecutionResult result)
                    super.onPostExecute(result);
                    callback.onTaskComplete(result);




                    Then use TaskExecutionResult in onTaskComplete:



                    public void onTaskComplete(TaskExecutionResult result) 
                    if (result.method.equals("GET"))
                    // Parse the GET result (from result.resultString)
                    else if(result.method.equals("POST"))
                    // Parse the POST result (from result.resultString)




                    This approach will also allow you to easily update the result object if you needed to add more return variables.






                    share|improve this answer

























                    • could you please elaborate more. Could not understand what you want say.

                      – rpurant
                      Mar 23 at 5:45











                    • @rpurant, I've added an example. Hope it helps

                      – foobar
                      Mar 24 at 10:44















                    0














                    I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask



                    Then the new class can then encapsulate the string you require along with other variables you would need.



                    For example:



                    // Create a new class to hold the result of `AsyncTask` execution
                    public class TaskExecutionResult
                    public String method;
                    public String resultString;


                    // When subclassing AsyncType assign TaskExecutionResult as return type
                    // Please replace URL with input parameters you require (perhaps a method string)
                    private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>

                    // doInBackground will now return TaskExecutionResult type of object
                    protected TaskExecutionResult doInBackground(URL... urls)
                    // Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
                    ...


                    // onPostExecute will also accept TaskExecutionResult type of object
                    protected void onPostExecute(TaskExecutionResult result)
                    super.onPostExecute(result);
                    callback.onTaskComplete(result);




                    Then use TaskExecutionResult in onTaskComplete:



                    public void onTaskComplete(TaskExecutionResult result) 
                    if (result.method.equals("GET"))
                    // Parse the GET result (from result.resultString)
                    else if(result.method.equals("POST"))
                    // Parse the POST result (from result.resultString)




                    This approach will also allow you to easily update the result object if you needed to add more return variables.






                    share|improve this answer

























                    • could you please elaborate more. Could not understand what you want say.

                      – rpurant
                      Mar 23 at 5:45











                    • @rpurant, I've added an example. Hope it helps

                      – foobar
                      Mar 24 at 10:44













                    0












                    0








                    0







                    I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask



                    Then the new class can then encapsulate the string you require along with other variables you would need.



                    For example:



                    // Create a new class to hold the result of `AsyncTask` execution
                    public class TaskExecutionResult
                    public String method;
                    public String resultString;


                    // When subclassing AsyncType assign TaskExecutionResult as return type
                    // Please replace URL with input parameters you require (perhaps a method string)
                    private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>

                    // doInBackground will now return TaskExecutionResult type of object
                    protected TaskExecutionResult doInBackground(URL... urls)
                    // Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
                    ...


                    // onPostExecute will also accept TaskExecutionResult type of object
                    protected void onPostExecute(TaskExecutionResult result)
                    super.onPostExecute(result);
                    callback.onTaskComplete(result);




                    Then use TaskExecutionResult in onTaskComplete:



                    public void onTaskComplete(TaskExecutionResult result) 
                    if (result.method.equals("GET"))
                    // Parse the GET result (from result.resultString)
                    else if(result.method.equals("POST"))
                    // Parse the POST result (from result.resultString)




                    This approach will also allow you to easily update the result object if you needed to add more return variables.






                    share|improve this answer















                    I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask



                    Then the new class can then encapsulate the string you require along with other variables you would need.



                    For example:



                    // Create a new class to hold the result of `AsyncTask` execution
                    public class TaskExecutionResult
                    public String method;
                    public String resultString;


                    // When subclassing AsyncType assign TaskExecutionResult as return type
                    // Please replace URL with input parameters you require (perhaps a method string)
                    private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>

                    // doInBackground will now return TaskExecutionResult type of object
                    protected TaskExecutionResult doInBackground(URL... urls)
                    // Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
                    ...


                    // onPostExecute will also accept TaskExecutionResult type of object
                    protected void onPostExecute(TaskExecutionResult result)
                    super.onPostExecute(result);
                    callback.onTaskComplete(result);




                    Then use TaskExecutionResult in onTaskComplete:



                    public void onTaskComplete(TaskExecutionResult result) 
                    if (result.method.equals("GET"))
                    // Parse the GET result (from result.resultString)
                    else if(result.method.equals("POST"))
                    // Parse the POST result (from result.resultString)




                    This approach will also allow you to easily update the result object if you needed to add more return variables.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 24 at 10:51

























                    answered Mar 22 at 17:43









                    foobarfoobar

                    435814




                    435814












                    • could you please elaborate more. Could not understand what you want say.

                      – rpurant
                      Mar 23 at 5:45











                    • @rpurant, I've added an example. Hope it helps

                      – foobar
                      Mar 24 at 10:44

















                    • could you please elaborate more. Could not understand what you want say.

                      – rpurant
                      Mar 23 at 5:45











                    • @rpurant, I've added an example. Hope it helps

                      – foobar
                      Mar 24 at 10:44
















                    could you please elaborate more. Could not understand what you want say.

                    – rpurant
                    Mar 23 at 5:45





                    could you please elaborate more. Could not understand what you want say.

                    – rpurant
                    Mar 23 at 5:45













                    @rpurant, I've added an example. Hope it helps

                    – foobar
                    Mar 24 at 10:44





                    @rpurant, I've added an example. Hope it helps

                    – foobar
                    Mar 24 at 10:44

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304926%2fhow-one-interface-can-be-used-for-more-than-two-background-android-tasks-within%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해