java - Jsoup.connect() 的 get 方法阻塞 UI

标签 java android android-asynctask

我创建了一个分为两部分的 android 应用程序:T

  • 管理器,使用 Jsoup.connect() 在网站上进行调用,与 AsyncTask 和所有覆盖方法关联。

  • 使用管理器调用方法和格式化答案的 Activity 。

我的问题是当我尝试在一个 Activity 中接听电话时。在接听电话之前,UI 将被阻塞。

阅读 StackOverflow 中的其他问题,我了解到问题出在我尝试调用 Jsoup.connect(values[0]).get();doInBackground( ) 方法,但我尝试遵循一些指南,但没有人起作用。

代码

Activity

public class CreazioneLibrettoActivity extends AppCompatActivity {

    private EditText editMatricola;
    private Spinner elencoPercorsi;
    private ListView elencoInsegnamenti;
    private String matricola;
    private PercorsiAdapter percorsiAdapter;
    private InsegnamentiAdapter insegnamentiAdapter;
    private ArrayList<Insegnamento> elencoEsami;
    private FloatingActionButton bottonPiano;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creazione_libretto);

        bottonPiano = findViewById(R.id.bottonPiano);
        editMatricola = findViewById(R.id.matricola);
        elencoPercorsi = findViewById(R.id.elencoPercorsi);
        elencoInsegnamenti = findViewById(R.id.elencoInsegnamenti);


        editMatricola.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if(charSequence.length() == 5) {

                    InputMethodManager imm = (InputMethodManager)getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(editMatricola.getWindowToken(), 0);
                    Esse3RecoveryManager esse3 = new Esse3RecoveryManager(getApplicationContext());
                    matricola = charSequence.toString();

                    String result = esse3.selectCdL(matricola);

                    if(result != null) {
                        if(result.equals("nullo")){
                            Toast.makeText(getApplicationContext(),"Nessuna connessione a internet",Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            TreeMap<String, String> percorsi = esse3.getPercorsi(result);
                            if(percorsi == null) {
                                System.out.println(percorsi);
                                Toast.makeText(getApplicationContext(),"Nessuna connessione a internet",Toast.LENGTH_LONG).show();
                            } else {
                                percorsiAdapter = new PercorsiAdapter(getApplicationContext(), R.layout.spinner_percorsi, percorsi);
                                elencoPercorsi.setAdapter(percorsiAdapter);
                                elencoPercorsi.setVisibility(View.VISIBLE);
                            }
                        }
                    } else {
                        Toast.makeText(getApplicationContext(),"Matricola non trovata",Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        elencoPercorsi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                String url = view.getTag().toString();

                if(!url.equals("firstTag")) {
                    Esse3RecoveryManager esse3 = new Esse3RecoveryManager(getApplicationContext());
                    elencoEsami = esse3.getCdS(url, matricola);
                    if (elencoEsami == null) {
                        Toast.makeText(getApplicationContext(), "Nessuna connessione a internet", Toast.LENGTH_LONG).show();
                    } else {
                        insegnamentiAdapter = new InsegnamentiAdapter(getApplicationContext(), R.layout.insegnamenti_listview, elencoEsami);
                        elencoInsegnamenti.setAdapter(insegnamentiAdapter);
                        elencoInsegnamenti.setVisibility(View.VISIBLE);
                        bottonPiano.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        bottonPiano.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), PianoActivity.class);
                i.putExtra("matricola", matricola);
                i.putExtra("elencoEsami", elencoEsami);
                startActivity(i);
            }
        });

    }


}

异步任务

public class Esse3RecoveryManager{

    private Context context;
    private static Document docInitial = null;
    private static final String URL_SELECT_CDL = "https://esse3web.unisa.it/Guide/ListaFacoltaCorsi.do";

    public Esse3RecoveryManager(Context context) {
        this.context = context;
    }

    /**
     * Metodo utilizzato per il recupero dell'url del sito web del corso di laurea scelto dall'utente.
     * @param matricola, la matricola del corso di laurea scelto dall'utente.
     * @return l'url del corso di laurea.
     */
    public String selectCdL(String matricola) {
        if(!networkConnectivity() || !isInternetWorking(URL_SELECT_CDL)) {
            return "nullo";
        }

        if(docInitial == null) {
            try {
                NetworkTask nt = new NetworkTask();
                docInitial = nt.execute(URL_SELECT_CDL).get();
            } catch (Exception e) {
                e.getMessage();
            }
        }
        Document doc = docInitial;
        Element a;
        if(doc.select("a:contains("+ matricola + ")").size() > 0)
        {

            a = doc.select("a:contains("+ matricola + ")").get(0);
            return "https://esse3web.unisa.it/" + a.attr("href");
        }
        return null;
    }

    /**
     * Metodo utilizzato per il recupero dei percorsi di studio relativi al corso di laurea scelto dall'utente.
     * @param url, l'url relativo al corso di laurea scelto dall'utente.
     * @return l'elenco dei percorsi di studio.
     */
    public TreeMap<String, String> getPercorsi(String url) {
        if(!networkConnectivity() || !isInternetWorking(url)) {
            return null;
        }

        NetworkTask nt = new NetworkTask();
        Document doc = null;
        try {
            doc = nt.execute(url).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Elements links_percorso = doc.select("a:contains(Ord.)");
        TreeMap<String, String> percorsi = new TreeMap<String, String>();
        percorsi.put("- - - Scegli il percorso di studi - - -","firstTag");
        for(Element link : links_percorso) {
            percorsi.put(link.text(), "https://esse3web.unisa.it/" + link.attr("href"));
        }

        return percorsi;
    }

    /**
     * Metodo utilizzato per il recupero della durata del CdL.
     * @param url, l'url del corso di laurea scelto dall'utente.
     * @return la durata del CdL.
     */
    public int durataCdL(String url) {
        NetworkTask nt = new NetworkTask();
        Document doc = null;
        try {
            doc = nt.execute(url).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        int durata = Integer.parseInt(doc.getElementsByTag("durata_effettiva").first().text());

        return durata;
    }

    /**
     * Metodo utilizzato per il recupero degli insegnamenti relativi al percorso di studi scelto.
     * @param url, l'url relativo all'elenco degli insegnamenti.
     * @param matricola, la matricola del corso di laurea scelto dall'utente.
     * @return l'elenco degli insegnamenti.
     */
    public ArrayList<Insegnamento> getCdS(String url, String matricola) {
        if(!networkConnectivity() || !isInternetWorking(url)) {
            return null;
        }

        NetworkTask nt = new NetworkTask();
        Document doc = null;
        try {
            doc = nt.execute(url).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        int durata = durataCdL(selectCdL(matricola));

        ArrayList<Insegnamento> insegnamenti = new ArrayList<Insegnamento>();
        long in = System.currentTimeMillis();
        for(int i=1; i<=durata; i++) {
            Element table = doc.getElementById("table1_" + i);
            Element tbody = table.getElementsByTag("tbody").first();
            Elements rows = tbody.children();
            for(Element row : rows) {
                Insegnamento ins = new Insegnamento();
                Elements data = row.getElementsByTag("td");
                Pattern pattern = Pattern.compile("\\[(.*?)\\]");
                Matcher matcher = pattern.matcher(data.get(0).text());
                if (matcher.find()) {
                    ins.setAd_insegnamento(matcher.group(1));
                }
                ins.setNome_insegnamento(data.get(0).text().substring(13));
                if(data.get(1).text().equals("S"))
                    ins.setObbligatorieta(true);
                else ins.setObbligatorieta(false);
                if(data.get(2).text().equals("PRIMO SEMESTRE"))
                    ins.setSemestre(1);
                else ins.setSemestre(2);
                ins.setCfu(Integer.parseInt(data.get(3).text()));
                ins.setSsd(data.get(4).text());
                ins.setTaf(data.get(5).text());
                ins.setAnno(i);
                insegnamenti.add(ins);
            }

        }
        long out = System.currentTimeMillis();

        return insegnamenti;
    }


    class NetworkTask extends AsyncTask<String, Integer, Document> {
        @Override
        protected Document doInBackground(String... values) {

            try {

                Document doc = Jsoup.connect(values[0]).get();
                return doc;

            } catch (IOException exception) {
                exception.printStackTrace();
            }
            return null;
        }
    }

    class NetworkTaskInternet extends AsyncTask<String, Integer, Boolean> {
        @Override
        protected Boolean doInBackground(String... values) {
            boolean success = false;
            try {
                URL url = new URL(values[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(10000);
                connection.connect();
                success = connection.getResponseCode() == 200;
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            return success;
        }
    }

    public boolean isInternetWorking(String url) {
        NetworkTaskInternet nti = new NetworkTaskInternet();
        boolean success = false;
        try {
            success = nti.execute(url).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }

    private boolean networkConnectivity() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }

}

最佳答案

这是因为您在 AsyncTask 上调用了 get()。这实际上使它变得无用,因为它在继续之前等待 AsyncTask 完成。

相反,使用回调。做一个简单的界面:

interface NetworkCallback {
    void onDocumentLoaded(Document document);
}

然后将其添加为 AsyncTask 的构造函数的参数,并将其分配给全局变量:

class NetworkTask extends AsyncTask<String, Integer, Document> {
    private NetworkCallback callback;

    public NetworkTask(NetworkCallback callback) {
        this.callback = callback;
    }
}

然后在 onPostExecute() 中调用该监听器:

class NetworkTask extends AsyncTask<String, Integer, Document> {
    private NetworkCallback callback;

    public NetworkTask(NetworkCallback callback) {
        this.callback = callback;
    }

    // your doInBackground logic

    @Override
    protected void onPostExecute(Document document) {
        callback.onDocumentLoaded(document);
    }
}

现在,当您创建 NetworkTask 的新实例时,它应该如下所示:

new NetworkTask(new NetworkCallback() {
    @Override
    public void onDocumentLoaded(Document document) {
        //do whatever you want to do with the document
    }).execute(url);

我建议您对所有 AsyncTasks 都这样做。您只需要为每个人制作一个适当的回调接口(interface)即可。

另外,让你的 AsyncTasks 静态化:

static class NetworkTask extends AsyncTask<String, Integer, Document>

关于java - Jsoup.connect() 的 get 方法阻塞 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213353/

相关文章:

java - 如何调用 Oracle 函数返回记录表

java - 如何使用 jorda 时间将时区转换为 UTC 时区

java - 如何动态刷新ListView?

java - 没有互联网连接时应用程序崩溃

java - Android GridView 适配器与 AsyncTask

java - GWT RPC - 多个 RPC 服务

java - fizzbuzz - 可以短一点吗?

java - String.format ("%.1g", 0.0f) 导致索引异常,这是一个错误吗?

首次启动 fragment 时,Android ListView 不显示内容

android - 如何强制客户端更新谷歌播放服务?