0

I can not understand whether I ever made? I have a class which takes a reference to the input and the output gives me an object Document(Jsoup library). I then in the main stream parsing the object and set on the form data. at the beginning of the work I show ProgressDialog and at the end of it I'm. But everything works is unclear. I run a process asinktask starts and the form freezes (about a second). Sidebar does not have time to close. Then form droops ProgressDialog appears for a split second and closed. The data are mounted on the form. Everything is very fast, but what form freezes and ProgressDialog started only after it droops worries me.

public class MyAsincTask extends AsyncTask<String, Void, Document> {
    private Document document;
    private ProgressDialog progressDialog;

    public MyAsincTask(Context context) {
        progressDialog = MyProgress.getProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected Document doInBackground(String... params) {
        try {
            document = Jsoup.connect(params[0]).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

    @Override
    protected void onPostExecute(Document document) {
        progressDialog.cancel();
        super.onPostExecute(document);
    }
}

and

 public void setData(){
            MyAsincTask task = new MyAsincTask(getActivity());
            task.execute(link);
            try {
                document = task.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            guests = new ArrayList<>();
            Elements elementsUserId = document.select("guest_id");
            Elements elementsNumbers = document.select("room");
            Elements elementsNames = document.select("name");
            ...


            for (int i = 0; i < elementsNumbers.size(); i++) {
                GuestBean guest = new GuestBean();
                guest.setUserId(elementsUserId.get(i).ownText());
                guest.setRoom(elementsNumbers.get(i).ownText());
                guest.setName(elementsNames.get(i).ownText());
                ...
                guests.add(guest);
                count++;
            }
            GuestsListAdapter adapter = new GuestsListAdapter(getActivity(), guests);
            list.setAdapter(adapter);
        }
Pavel Petrashov
  • 205
  • 2
  • 10
  • 3
    `run a process as in task starts and the form freezes (about a second)` because of `document = task.get();`. line see [AsyncTask.get()](http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29) for more detail. – ρяσѕρєя K Dec 08 '14 at 09:40
  • I know that hangs therefore. the question is whether it is right? task.get() waits when AsyncTask finish work. and while waiting for the main thread hangs. I need to at this time spinning my progressBar.it can? – Pavel Petrashov Dec 08 '14 at 09:49
  • `I need to at this time spinning my progressBar` to do same use `onPostExecute` for performing options on result of `doInBackground`. see [Android AsyncTask ListView – JSON](http://www.javacodegeeks.com/2013/06/android-asynctask-listview-json.html) example for more help – ρяσѕρєя K Dec 08 '14 at 09:55
  • @PavelPetrashov : NEVER use the `get()` method of `AsyncTask`. I really wish the Android devs would remove it as it only causes confusion for people who don't understand the implications. Move the code in your setData method into `onPostExecute(..)` instead. – Squonk Dec 08 '14 at 09:55
  • @Squonk I did so. but I wanted to use a single class AsyncTask for all fragments. – Pavel Petrashov Dec 08 '14 at 10:01
  • @PavelPetrashov : Using a stand-alone `AsyncTask` with different app components (e.g., Activities or Fragments) gets complicated and is potentially unsafe when used by an `Activity` if the device is rotated. If I need to use an `AsyncTask` I usually make it an inner class of a base `Activity` or base `Fragment` and then extend those classes. Alternatively, I prefer to use an `IntentService` for most asynchronous operations. – Squonk Dec 08 '14 at 10:06

2 Answers2

0

AS @ρяσѕρєя K said above the problem is document = task.get();, Here the thread will get blocked.You can done this by using an Interface like this.

Community
  • 1
  • 1
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
0

I found the solution

 public void setData() {
        new MyAsincTask(getActivity()){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Document document) {
                super.onPostExecute(document);
                Elements elementsDate = document.select("date");
                Elements elementsName = document.select("name");
                Elements elementsSumm = document.select("summa");

                for (int i = 0; i < elementsName.size(); i++) {
                    FolioBean folioBean = new FolioBean();
                    folioBean.setDate(elementsDate.get(i).ownText());
                    folioBean.setName(elementsName.get(i).ownText());
                    folioBean.setSumm(elementsSumm.get(i).ownText());
                    folioBeans.add(folioBean);
                }
                TableRow.LayoutParams row_params = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 20f);
                TableRow.LayoutParams row_params2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 30f);

                for (FolioBean folioBean : folioBeans) {
                    TableRow tableRow = new TableRow(getActivity());
                    TextView data = new TextView(getActivity());
                    TextView name = new TextView(getActivity());
                    TextView summ = new TextView(getActivity());
                    data.setTextSize(10);
                    name.setTextSize(10);
                    summ.setTextSize(10);
                    name.setTextColor(Color.BLACK);
                    summ.setTextColor(Color.BLACK);
                    data.setTextColor(Color.BLACK);
                    data.setText(" " + folioBean.getDate());
                    name.setText(" " + folioBean.getName() + " ");
                    summ.setText(folioBean.getSumm() + " ");
                    tableRow.addView(data, row_params2);
                    tableRow.addView(name, row_params2);
                    tableRow.addView(summ, row_params);
                    tableFolio.addView(tableRow);
                }
            }
        }.execute(link);
    }
Pavel Petrashov
  • 205
  • 2
  • 10