Search code examples
javaandroidjsoup

Why the text is displayed on one line?


Well, I have a problem with my code, I need to download the code from the URL (code: php), using JSOUP on the Android platform. The site looks nice (data is separated by ENTER), and in the application everything is written in one String and I can not adjust it to your needs. Namely, I would like the data from the page to be identically arranged in the application.

private void getWebsite(){
         new Thread(new Runnable() {
        @Override
        public void run() {
            final StringBuilder builder = new StringBuilder();

            try {

                Document doc = Jsoup.connect("https://k69.pl/odtwarzacz/aplikacjaRadia.php").get();

                Elements links = ((Document) doc).select("body");


                    for(Element link : links){

                    builder.append(link.attr("<br>")).append(link.text());


                }

            } catch (IOException e){
                builder.append("Aplikacja nie mogła wykonać żądania. Zgłoś ten błąd do administratora aplikacji. Twój błąd to : ").append(e.getMessage()).append("\n");
            }


            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    wynik.setText(builder.toString());
                }
            });

        }
    }).start();




}

Result from the site:

https://i.sstatic.net/mvIbe.jpg

Result from the app:

https://i.sstatic.net/VADVI.jpg


Solution

  • is for HTML so if you are building HTML for a webview that will work fine. However, you are setting a Text so HTML tags are not understood or obeyed.

    Instead for setting text you should be doing

    builder.append(link.attr("\n")).append(link.text());
    

    Happy Coding!