5

say I want to perform an Http request from the server, this process takes time.

now because of this, the http request needs to be run on a different thread (AsyncTask, Runnable, etc.)

but sometimes I just need the response when I ask for it, in order to update the UI

using Thread.sleep in a loop to wait for the response is not good performance wise

example: I want the user's name, I ask the server for it, and I have to wait for it now the activity calls the UserManager that calls the serverHandler that performs the operation and returns the result back

maybe an event system is in order, but I'm not sure how to do this in my scenerio and quite frankly I am really confused on this issue

please help someone?

Amir.F
  • 1,911
  • 7
  • 29
  • 52
  • 1
    what is the problem using asynctask. it will do that for you – stinepike Oct 07 '13 at 15:54
  • async task would be easy if I were to do it from the activity, and then update the UI when it's done, but my system is more complicated then that, and it has layers (activity->UserManager->ServerHandler) – Amir.F Oct 07 '13 at 15:55
  • the GUI separation from the rest of the system is what confuses me in this case, and makes it harder for me to figure out – Amir.F Oct 07 '13 at 15:57
  • You can use different threads then... may be i am not getting you right – stinepike Oct 07 '13 at 15:59
  • the activity sees its UI controls, it asks the usermanager for the user name which then goes to the ServerHandler which does an Http request on a different thread, if I wait for that thread to finish there's no point to it all, it stalls the UI, so I need to find a way to do it with events & maybe a queue of http requests – Amir.F Oct 07 '13 at 16:04
  • You can check out this solution, http://stackoverflow.com/a/37319175/2866395. – padfoot27 May 19 '16 at 09:33

1 Answers1

6

This can most definitely be done w/ AsyncTask... Handle the network request in doInBackground() and once doInBackground() is finished, onPostExecute() is triggered on the UI thread and that's where you can execute any code that will update UI elements.

If you need something a bit more generic and re-usable, you would probably want to implement a callback... I'll refer to the UI thread as the client and the AsyncTask as the server.

  1. Create a new interface and create some method stubs.

    public interface MyEventListener {
        public void onEventCompleted();
        public void onEventFailed();
    } 
    
  2. Have your client pass instance of MyEventListener to the server. A typical way of doing this is to have your client implement the interface (MyEventListener) and pass itself to the server.

    public class MyActivity implement MyEventListener {
    
        public void startEvent() {
            new MyAsyncTask(this).execute();
        }           
    
        @Override
        public void onEventCompleted() {
            // TODO
        }
    
        @Override
        public void onEventFailed() {
            // TODO
        }
    }
    
  3. On the onPostExecute of the server, check if the callback is null and call the appropriate method.

    public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        private MyEventListener callback;
    
        public MyAsyncTask(MyEventListener cb) {
            callback = cb;
        }
    
        [...]
    
        @Override
        protected void onPostExecute(Void aVoid) {
            if(callback != null) {
                callback.onEventCompleted();
            }
        }
    }
    

You can read more about callbacks here: http://www.javaworld.com/javaworld/javatips/jw-javatip10.html

Alex Fu
  • 5,509
  • 3
  • 31
  • 40
  • 1
    exactly..only one thing before I approve your answer..that means I have to write different events for each and every different request? – Amir.F Oct 07 '13 at 16:23
  • 1
    No, you don't have to. If you're making a Http request to two different end points, you can use the same callback. The only thing different you would need to do is implement some logic to differentiate the difference between the two. Like using a request ID or something similar. – Alex Fu Oct 07 '13 at 16:29