Christian
Christian

Reputation: 53

How I can get information from a tag in HTML WebView?

I would like to know how I can get a information inside a tag in HTML. I don't know if I am doing it right because It don't return any information. I show you my android code to see if you can help me.

code class:

    public class WebView1 extends Activity {
        /** Called when the activity is first created. */   
WebView browse;         
        @Override
        protected void onCreate(Bundle savedInstanceState)      {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.webview1);
         browse = (WebView) findViewById(R.id.webview1);                     

         browse.setWebChromeClient(new WebChromeClient());

         browse.setWebViewClient(new WebViewClient() {
                @Override  
                public void onPageFinished(WebView view, String url) {
                    File input = new File("file:///android_asset/ejemploWebview.html");
                    Document doc = null;
                    try {
                        doc = Jsoup.parse(input, "UTF-8");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    //http://jsoup.org/cookbook/input/load-document-from-url
                    //Document doc = Jsoup.connect("http://example.com/").get();

                    Element content = doc.getElementById("div");
                    Elements links = content.getElementsByTag("id");
                    String linkId = links.attr("manolo");
                    System.out.print(linkId); //I need that it return Hiiii!
                    }
            });     } }

code HTML:

 <html>
    <head>
    </head>
    <div id="james">hellooo!</div>
    <div id="paco">byeee!</div>
    <div id="manolo">Hiii!</div>
    </html>

I hope I explained correctly! Thank's you! ;)

Upvotes: 2

Views: 2590

Answers (1)

Jimmy
Jimmy

Reputation: 16428

Personally, I've always had better results from JSoup when using the selectors, as detailed here.

From the example you give, it appears you want to retrieve the div values by their ID, you can use this :

el#id: elements with ID, e.g. div#logo

So either use 3 occurrences of the above, or just select the divs and iterate over them doing whatever you need to do.

Hope this helps.

P.S, easiest thing I found was to put a breakpoint after you call doc = Jsoup.parse(input, "UTF-8");, then use your IDEs' expression builder to work out which selector does what you want :)

Upvotes: 1

Related Questions