He seemed to be doing everything according to the instructions. In Android, the development of a full amateur, trying to write my first application - a client for the site. How to solve a problem? it seems to be sharing actions by internal classes, anyway, an error with the threads ..

package client.adme.ru.websiteparsing; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutionException; public class MainActivity extends AppCompatActivity { private ListView listView; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); textView = (TextView) findViewById(R.id.textView); ParseTitle parseTitle = new ParseTitle(); parseTitle.execute(); try { final HashMap<String, String> hashMap = parseTitle.get(); final ArrayList<String > arrayList = new ArrayList<>(); for (Map.Entry entry : hashMap.entrySet()) { arrayList.add(entry.getKey().toString()); } ArrayAdapter<String > arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arrayList); listView.setAdapter(arrayAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ParseText parseText = new ParseText(); parseText.execute(hashMap.get(arrayList.get(position))); try { listView.setVisibility(View.GONE); textView.setText(parseText.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } @Override public void onBackPressed() { listView.setVisibility(View.VISIBLE); textView.setVisibility(View.GONE); } class ParseText extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { String str = " "; try { Document doc = Jsoup.connect(params[0]).get(); Element element = doc.select(".wrap.footer-placeholder").first(); str = element.text(); } catch (IOException e) { e.printStackTrace(); } return str; } } class ParseTitle extends AsyncTask<Void, Void, HashMap<String, String>> { @Override protected HashMap<String, String> doInBackground(Void... params) { HashMap<String , String > hashMap = new HashMap<>(); try { Document doc = Jsoup.connect("http://www.adme.ru/").get(); Elements elements = doc.select(".al-title"); for (Element element : elements ) { Element element1 = element.select("a[href]").first(); hashMap.put(element.text(), element1.attr("abs:href")); } } catch (IOException e) { e.printStackTrace(); } return hashMap; } } } 

    1 answer 1

    The parseTitle.get() method hangs the main thread for a while, until parseTitle.execute() .
    In order not to hang the main thread, override the onPostExecute() method of onPostExecute() . This method will be called in the main thread after the doInBackground() method completes.

    • thanks, I'll try now! - Vladislav