Skip to content
Advertisement

I have a function, and in it i am running an thread using Anonymous class, so how to return value to that function

The code below will make it more clear:

JavaScript

So as you can see from the code, there is a function, TCMResponse() which takes the parameters of the url which i pass, and it does web scraping, i know all these can be done using volley/ JSONParser easily. But i am just experimenting, how to parse using web scraping.

So after the page is scraped, i need that function to return the response of the scraped page,

I’ve used Callable with executor service, but it again freezes the thread.. Have a look on what i’ve done:

JavaScript

Advertisement

Answer

The main thread gets blocked because of your call to Future::get().

From the docs:

Waits if necessary for the computation to complete, and then retrieves its result.

which means; if the task Thread has not yet finished, the current Thread will wait until it returns a result.


I can see another problem in your code: you are showing a Snackbar, which is a UI component, in a Thread that is not the UI Thread.

Since you are working on Android, I would definitely use an AsyncTask, perform the expensive call in doInBackground(), then update the UI in onPostExecute().

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement