2

What I want to do should be very simple, however I have little to none Java/Android experience.

I have a WebView setup in Eclipse. I want the webpage to go to http://example.com/mypage.php. On that page will be either the text 'OK' or 'ALERT'.

I need to know how to check the page for those words. That will be all that's on the page.

Any help please? Thanks!

DannyF247
  • 628
  • 4
  • 14
  • 35
  • http://stackoverflow.com/questions/4457492/simple-http-example-in-android – ariefbayu Dec 21 '11 at 07:13
  • If you just want to check the result for `OK` or `ALERT`, why do you need the WebView? Can't you just request the url through http and then load your result in a string and `.equals()` that? – Nanne Dec 21 '11 at 15:17
  • @Nanne, that would work just fine and I'd prefer that. – DannyF247 Dec 22 '11 at 05:22

1 Answers1

3

There's no straightforward way to do what you want, though it can be done. First, define an interface that goes something like:

public interface WebInterfaceDelegate {
    public void receivedHtml(String html);
}

Then, define a class that you want to expose to the JavaScript runtime in your WebView, something along the lines of:

public static class WebViewJavaScriptInterface {
    private WebInterfaceDelegate delegate;

    public WebViewJavaScriptInterface(WebInterfaceDelegate delegate) {
        this.delegate = delegate;
    }

    public void htmlLoaded(String html) {
        if (delegate != null) {
            delegate.receivedHtml(html);
        }
    }
}

Then, make your Activity implement the interface you defined above, like:

public class AndroidWebViewActivity extends Activity implements WebInterfaceDelegate {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //...
        WebView webView = (WebView)findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "AndroidAPI");

        webView.loadUrl("http://your.url.com");
    }

    //...

    public void receivedHtml(String html) {
        //inspect the HTML here
        System.out.println("Got html:  " + html);
    }
}

Finally, add some JavaScript to your page that goes like:

<script>
    window.onload = function() {
        AndroidAPI.htmlLoaded(document.body.innerHTML);
    }
</script>

So you can do all that, or you can just use something like URL.openStream() to connect directly to your target webpage and read the markup off of the socket instead of off of the WebView. To do this is as simple as:

InputStream response = new URL("http://example.com/mypage.php").openStream();
//read the data from the response into a buffer, then inspect the buffer

You can find some additional information here:

http://docs.oracle.com/javase/6/docs/api/java/net/URL.html

aroth
  • 54,026
  • 20
  • 135
  • 176
  • I looked over all of it, but I don't really need the webview. I tried to find some sort of documentation for URL.openStream() but I couldn't find anything, could you please explain more about that method? – DannyF247 Dec 21 '11 at 20:04
  • @DannyF247 - Okay, I added a bit more information about the `URL.openConnection()` approach. It's much simpler than trying to go through the `WebView`. – aroth Dec 22 '11 at 01:29
  • I'm an extreme noob, I really do appreciate your help. I've done that, but I get `code`Unhandled exception type IOException`code`. I've tried adding `throws IOException` part in the `public void onCreate` part but that just gives me more errors. =/ – DannyF247 Dec 22 '11 at 04:14