简体   繁体   中英

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://imgur.com/a/LrE3oOH

Result from the app:

https://imgur.com/a/IlotHa6

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!

                try {

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

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


                    for(Element link : links){

                        builder.append(links.attr("\n")).append(links);

Sam, I changed the code, now I have such a result ( https://imgur.com/a/ts8FPhJ ). How to get rid of this div and br?

link.text() <= causes the text to continue in one line

Thanks for help, I did it differently and it works as I wanted. Swap Elemens "JSOUP" on String.

 for(Element link : links){

                       String tresc = links.toString();

                       String[] x;
                       String zamien;

                       zamien =  tresc.replace("<br>","");
                       x = zamien.split("\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM