4

I'm trying to bring a part of the contents of a website. I decided to use the Jsoup because with it I can take the part of the site you want and play the html part of this withdrawal within a webview. It seemed easy, but I tried to run my application and did not work. Below is my code. What's wrong with him?

Note: If there is another simpler way to do this, pass me the hint! But I want to get the contents in real time, ok? Thanks to all!

public class RestauranteUniversitarioActivity 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("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");
} 

}

Community
  • 1
  • 1

3 Answers3

2

The problem is most likely that you're parsing the webpage in the main (UI) Thread. This will lock up the UI/cause your app to freeze. Move the doc = Jsoup.connect("http://www.iguatu.ce.gov.br/").get(); to a background thread, for example by using an AsyncTask or (Intent)Service.

Reinier
  • 3,836
  • 1
  • 27
  • 28
1

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();
        }
    } 
}

Reference Jsoup Cookbook

Community
  • 1
  • 1
dira
  • 30,304
  • 14
  • 54
  • 69
  • From Alessandro Forcuti: Please check url in Jsoup.connect(...) and replace "androi-" with "android-" – rghome May 18 '17 at 07:39
0

Unfortunately I do not have enough reputation for comment under answer of Dira. Please check url in Jsoup.connect(...) and replace "androi-" with "android-"