The question is, I have asynctask, in which I copy text from a certain web page when I click a button, but I also need to parse another page when I click the second button, what do they do in this case? thank.

    1 answer 1

    In the case of AsyncTask you need to create a new instance of the task and run it. Those. It is not necessary to use the already completed task again, and it will not even work out.

    Also try more modern and trendy ways to communicate with the network. For example, a bunch of OkHttp , RxJava and Retrolambda . With them, getting an HTML page is just 4 lines:

     Observable.fromCallable(() -> new OkHttpClient.Builder().build().newCall(new Request.Builder().url("https://kremlin.ru").build()).execute().body().string()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(response -> Log.d("html: " + response ), error -> Log.e("error", e)); 

    The code above downloads the HTML from the site outside the main stream and outputs it to the console already in the main stream. In case of an error, it will also be displayed in the console. In the same style, you can do the parsing of the village.

    • I have to click on the first button to log in asynctask, and then on the second button I have to get the text - SWR
    • @SWR, this comment is yours does not carry information useful complementary question. I suspect that you did not see the answer to your question in the answer. Perhaps this is due to the vagueness of the wording. Try to reformulate the question. At the moment, it is not very clear what your problem is. - Yuriy SPb
    • I have 2 tasks: the first is to log in by the button, the second is to parse the text when you click on the button from another page, I don’t understand how to create 2 asynctask (different tasks will be in the doitbackground)? I'm trying to do this now: I have the first task in asynctask, and I implement the second through thread, using runnable - SWR
    • one
      @SWR, you can simply create two different AsyncTask descendant AsyncTask - they will have their own method implementations. And accordingly call the right place - YurySPb
    • one
      @SWR, you can, for example, call button.setEnable(false) and then, in the onPostExecute() method, onPostExecute() it back to enable button.setEnable(true) - YuriSPb