In android i'm using "import android.os.Handler;" and calling "handler.post(runnable);" from thread to continue my work on UI(Main) Thread. Is it any analogue of Handler or AsyncTask in Java? For example if i need to send any data to server - i must do it in background task. So, after receiving response - how can i send it to Main Thread?
Asked
Active
Viewed 184 times
1
-
You can use JDK [`HttpURLConnection`](http://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html) or some third-party library, e. g. Apache HTTP Client. To execute tasks asynchronyously, use `Thread` or `Executor`-- you can construct one using [`Executors`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html) – Alex Salauyou May 08 '16 at 14:34
-
I'm already using HttpURLConnection and Executors too, with fixed thread pool size. executor.submit() bla bla bla.. So, execute method starts Runnable in separate thread not in main Thread. How can i go back to Main Thread from Separate one? It's very easy on android. What is about NetBeans Java? – Rahim Rahimov May 08 '16 at 14:40
-
1It depends on what you need to do after response arrives. You can put all necessary logic directly in runnable you send to executor, or submit a callable and "freeze" the main thread until it is processed. – Alex Salauyou May 08 '16 at 14:43
-
To get a better response here, please formulate you particular problem, maybe attaching some pieces of code that you tried, describing desired behaviour and concrete issues you face. – Alex Salauyou May 08 '16 at 14:44
-
Yes, but i can't put all necessary logic directly in runnable, so freeze is not good too. In android i just calling handler.post(Runnable) from thread to start this runnable inside of Main Thread not in separate one. I need this)) need to run new Runnable on Main Thread from Separate Thread which was started by calling executor.submit(); (in Main Thread)... – Rahim Rahimov May 08 '16 at 15:30
-
or.. check this. http://stackoverflow.com/a/9671602/4576879 . In AsyncTask doInBackground method is in new thread and onPostExecute is on Main Thread. Handler.post works like onPostExecute – Rahim Rahimov May 08 '16 at 15:34