0

I am a newbee in Android and I am coding a news app. I did all the xml parsing stuff, for parsing title and description of news from rss feed of the news web page. And then I want to parse the news body from html and show it in a Textview. But I couldn't make it. Please help me, the html format that I am trying to parse is like this:

<div class="classname"> some text </div><p> some text</p><p> some text</p><h2> some text </h2><p> some text </p>

My question is "how can I get the whole news text with jsoup html parsing in this case?"

I searched in the internet and learned a bit of jsoup html parsing, and write some code. But I can parse only <div class="classname"> some text </div> part of the text. Please help me to parse whole news text.

My code is here:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView txtview = (TextView) findViewById(R.id.txtview);

    new FetchText().execute();

}

private class FetchText extends AsyncTask<Void,Void,Void>{
    String yazi;
    @Override
    protected Void doInBackground(Void... params) {

        try{
            org.jsoup.nodes.Document doc = Jsoup.connect("http://newswebsite.com/bla/bla").get();
            Elements elementdetail = doc.select("div[class=detail-news-spot mobile-spot]");
            yazi = elementdetail.text();
            Elements 

            }catch (Exception e){

            e.printStackTrace();
            }


        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {

        TextView txt_yazarlar = (TextView)findViewById(R.id.txtview);

        txt_yazarlar.setText(yazi);
        }
}

}

  • I'm guessing that your real HTML looks different from the sample you've provided, so the answer depends on the real structure you have. To parse your example you can use: Elements el = doc.select("p"); for (Element p : el) { system.out.println(p.text); } – TDG Sep 13 '16 at 18:40
  • Your page is probably build dynamically with javascript. You can verify that by disabling javascript in your browser and load the page. If that is the case, you can load the page in a js enabled WebView and parse the result with jsoup: http://stackoverflow.com/a/39174441/1661938 – Frederic Klein Sep 16 '16 at 06:46
  • I don't want to use Webview in my project, just because I want to parse html and show the text that is in web page, in edit text, not in webview. – Mehmet Sefa Balık Sep 18 '16 at 19:48

0 Answers0