2

I have a WebView in which I display my site, in that site I have 3 pages in sequence, of those 3 pages I need to identify when the user is on the last page, and from that to get the html content that display that page.

I manage to do a WebViewClient in which i can now identify when the user is on the last page, the problem is that I don t know how to move pass that, I don 't know how to get the html content from that page.

This is what I have so far:

namespace WebViewExample
{
  [Activity(Label = "WebView", MainLauncher = true)]
  public class WebView : Activity
{
    Android.Webkit.WebView web_view;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.WebView);

        web_view = FindViewById<Android.Webkit.WebView>(Resource.Id.webview);
        web_view.Settings.JavaScriptEnabled = true;
        web_view.SetWebViewClient(new MyWebViewClient());

        web_view.LoadUrl(url);
    }

    public class MyWebViewClient : WebViewClient
    {

        public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
        {
            view.LoadUrl(url);
            return true;
        }

        public override void OnPageStarted(Android.Webkit.WebView view, string url, Android.Graphics.Bitmap favicon)
        {
            base.OnPageStarted(view, url, favicon);
        }

        public override void OnPageFinished(Android.Webkit.WebView view, string url)
        {
            base.OnPageFinished(view, url);
            int i = 0;
        }


    }




}

}

Thanks.

1 Answer 1

2
  1. Create a c# class that contains methods to be called from javaScript
  2. If your Android API level 17 or later, This class need annotate each JavaScript-callable method with [JavascriptInterface] and [Export]
  3. Your WebView should call AddJavascriptInterface() method
  4. The method of onPageFinished() will be called when page loading finished

This is code :

namespace WebViewTest{
[Activity(Label = "WebViewTest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    Android.Webkit.WebView web_view;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        web_view = FindViewById<Android.Webkit.WebView>(Resource.Id.webview);
        web_view.Settings.JavaScriptEnabled = true;
        web_view.SetWebViewClient(new MyWebViewClient());
        web_view.LoadUrl("http://stackoverflow.com/questions/40943265/xamarin-android-get-html-content");
        // 3. your WebView must call AddJavascriptInterface() method 
        web_view.AddJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
    }
}

public class MyWebViewClient : WebViewClient
{
    private int count;
    public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
    {
        view.LoadUrl(url);
        return true;
    }

    public override void OnPageStarted(Android.Webkit.WebView view, string url, Android.Graphics.Bitmap favicon)
    {
        base.OnPageStarted(view, url, favicon);

    }

    public override void OnPageFinished(Android.Webkit.WebView view, string url)
    {
        base.OnPageFinished(view, url);
        count++;
        //page count 
        //  4.The method of onPageFinished() will be called when page loading finished
        if (count == 2)
        {
            view.LoadUrl("javascript:window.local_obj.showSource('<head>'+"
            + "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
            count = 0;
        }
    }
}
//1. Create a c# class that contains methods to be called from javaScript.
//The Method is that called in javaScript.
public sealed class InJavaScriptLocalObj : Java.Lang.Object
{
    //2.if your Android API level 17 or later, 
    //This class must annotate each JavaScript-callable method with [JavascriptInterface] and [Export]
    [Export]
    [JavascriptInterface]

    public void showSource(string html)
    {
        // System.out.println("====>html=" + html);
    }
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.