All Things Techie With Huge, Unstructured, Intuitive Leaps

Using the Android Simulator Webview, with Localhost and Apache Tomcat

Okay, let's suppose that you are developing an Android app with server support.  Let's suppose that you are using the server to securely access a database and you are using JSPs to do it, since Android is Java and you know Java.  So using Tomcat (what else) to serve up the JSPs, you want to do the development running Tomcat on your computer.  When you fire up your simulator to test the webview app and try to access the web url using http://localhost:8080, nothing happens.

Why?  Because the simulator is using localhost.  127.0.0.1 doesn't work either.  The simulator acts as a router, so if you want to access http://localhost:8080/index.jsp the way that you would do it in a browser, the URL host in the activity has to appear as 10.0.2.2 as the IP address.  So in the above example, you would use http://10.0.2.2:8080/index.jsp.

If you are like me, you have a separate Eclipse for Android development.  You can either start Tomcat using the service control panel, or you can invoke both the Android Eclipse SDK and the JSP Eclipse SDK.  I don't try to combine the two with two separate projects just for simplicity sake.

And that brings us to last item of integrating webview with an Android app.  Suppose you have a webview page and you want to trigger an activity with a URL instead of going to another JSP.  Easy peasy.

In your activity where your webview is, find the following bit of code:

WebView webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });


This is a standard bit of code for displaying a URL from a web server in webview.  To make a url do an activity, you modify it easily.  The first thing that you do, is that you signal an activity in the URL.  One way is to construct the url like:   <a href="app://signal_activity">

Then modify the above method to catch that string:

 WebView webview = (WebView) findViewById(R.id.webview);
       webview.setWebViewClient(new WebViewClient() {
           @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url) {
              // view.loadUrl(url);
            if (url.contains("app://signal_activity")){
                    
                   //trigger an Android action...,

               return true;
            }
            return super.shouldOverrideUrlLoading(view, url); 
           }
       });

There you go -- everything that you need to know to (1) use your local machine as the web server (2) use Tomcat to serve up the web stuff for the Android web view (3) use your Android simulator with your local machine and (4) integrate web pages with Android activities.

Hope this helps.

1 comment:

  1. Can we access localhost in my phones webview?

    ReplyDelete