Search code examples
javahtmlandroid-studiojsoup

how to get parse this span id element with jsoup in android studio?


I want to parse the span id = shd2b from this url in my android studio project, https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html

Screenshot in link below

I used the below java code to parse it and display in a textview box.

@Override
        protected Void doInBackground(Void... voids) {
            //Connect to the website
            String url = "https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html";
            //Get the title of the website
            Document doc = null;
            doc = (Document) Jsoup.parse(url);
            eurotoinr = doc.getElementById("span.shd2b");
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressDialog.dismiss();
            textView.setText((CharSequence) eurotoinr);
        }

Bu nothing is displayed in the emulator device when i run it. Could someone please help me with this issue?

Please forgive me if my description is not clear or not understandable. I started learning android studio only during this lockdown and new to stack overflow.


Solution

  • I see two mistakes.

    1. Did you try to debug or display how your document looks after parsing? It look like this:
    <html>
     <head></head>
     <body>
      https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html
     </body>
    </html>
    

    because you used wrong method and your string is parsed as HTML fragment. It doesn't try do download anything.
    You should use

    doc = Jsoup.connect(url).get();
    

    Then it downloads the HTML and parses correctly.

    1. Your code doc.getElementById("span.shd2b"); will return null because span.shd2b is not a valid id. The id is shd2b; Use doc.getElementById("shd2b;"); and it selects expected element:
    <span id="shd2b;">82.9337</span>