Android and Jsoup

I am trying to cite some of the contents of a website. I decided to use Jsoup because with it I can take part of the site that you want and play the html part of this output in a web review. It seemed easy to me, but I tried to run my application and did not work. Below is my code. What is wrong with him?

Note. If there is another easier way to do this, give me a hint! But I want to get the content in real time, okay? Thanks everyone!

public class RestauranteUniversitarioActivity extends the action {

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView cardapio = (WebView)findViewById(R.id.web_engine); cardapio.getSettings().setJavaScriptEnabled(true); String data = ""; Document doc = null; try { doc = Jsoup.connect("http://www.iguatu.ce.gov.br/").get(); } catch (IOException e) { e.printStackTrace(); } Elements content = doc.getElementsByClass("dest-left"); if(content.size() > 0) { data = content.get(1).text(); } cardapio.loadData(data, "text/html", "UTF-8"); } 

}

+4
source share
3 answers

Most likely, the problem is that you are parsing the web page in the main thread (UI). This will block the user interface / cause your application to freeze. Move doc = Jsoup.connect("http://www.iguatu.ce.gov.br/").get(); into a background thread, for example using AsyncTask or (Intent)Service .

+2
source

Refer this thread if you are looking for an alternative html parser.

 import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class HtmlParserActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView cardapio = (WebView) findViewById(R.id.web_engine); cardapio.getSettings().setJavaScriptEnabled(true); String data = ""; Document doc = null; try { doc = Jsoup.connect("https://stackoverflow.com/questions/10695350/androi-and-jsoup").get(); Elements elements = doc.getElementsByClass("post-tag"); for(Element element : elements) { data += element.outerHtml(); data += "<br/>"; } cardapio.loadData(data, "text/html", "UTF-8"); } catch (IOException e) { e.printStackTrace(); } } } 

Jwoup Cookbook Link

+1
source

Unfortunately, I do not have enough reputation to comment on Dira’s response. Check the url in Jsoup.connect (...) and replace "androi-" with "android -"

0
source

Source: https://habr.com/ru/post/1413651/


All Articles