Parse HTML in Android

前端 未结 5 792
逝去的感伤
逝去的感伤 2020-11-22 06:22

I am trying to parse HTML in android from a webpage, and since the webpage it not well formed, I get SAXException.

Is there a way to parse HTML in Andro

5条回答
  •  醉酒成梦
    2020-11-22 06:49

    We all know that programming have endless possibilities.There are numbers of solutions available for a single problem so i think all of the above solutions are perfect and may be helpful for someone but for me this one save my day..

    So Code goes like this

      private void getWebsite() {
        new Thread(new Runnable() {
          @Override
          public void run() {
            final StringBuilder builder = new StringBuilder();
    
            try {
              Document doc = Jsoup.connect("http://www.ssaurel.com/blog").get();
              String title = doc.title();
              Elements links = doc.select("a[href]");
    
              builder.append(title).append("\n");
    
              for (Element link : links) {
                builder.append("\n").append("Link : ").append(link.attr("href"))
                .append("\n").append("Text : ").append(link.text());
              }
            } catch (IOException e) {
              builder.append("Error : ").append(e.getMessage()).append("\n");
            }
    
            runOnUiThread(new Runnable() {
              @Override
              public void run() {
                result.setText(builder.toString());
              }
            });
          }
        }).start();
      }
    

    You just have to call the above function in onCreate Method of your MainActivity

    I hope this one is also helpful for you guys.

    Also read the original blog at Medium

提交回复
热议问题