6

I'm working with Android and Phonegap, and at the moment I'm having trouble with one simple thing. I need to setup a webViewClient to the PhoneGap webView in order to capture the URL of a page finished and to work with that.

This is the code:

public class PhoneGapTest extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.setBooleanProperty("loadInWebView", true);
        super.clearCache();
        super.keepRunning = false; 
        super.loadUrl("file:///android_asset/www/index.html");

        super.appView.setWebViewClient(new WebViewClient(){

          @Override
          public void onPageStarted(WebView view, String url, Bitmap bitmap) {

            Log.i("TEST", "onPageStarted: " + url);

          }

          @Override
          public void onPageFinished(WebView view, String url) {

            Log.i("TEST", "onPageFinished: " + url);

          }

        });

    }

That code doesn't seems to work, the page never loads and I get a TIMEOUT ERROR, but if I remove the "setWebViewClient" part the page loads perfectly.

I saw that there is a class CordovaWebViewClient, do I have to use that instead of WebViewClient? I found this way on the web:

        this.appView.setWebViewClient(new CordovaWebViewClient(this){

          @Override
          public boolean shouldOverrideUrlLoading(final WebView view, String url) { 
            Log.i("BugTest", "shouldOverrideUrlLoading: " + url); 
            return true; 
          }

          @Override
          public void onPageStarted(WebView view, String url, Bitmap bitmap) {

            Log.i("TEST", "onPageStarted: " + url);

          }

          @Override
          public void onPageFinished(WebView view, String url) {

            Log.i("TEST", "onPageFinished: " + url);

          }

          @Override
          public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){        
          }

        });

But that code isn't working either, I still got a TIMEOUT ERROR. I also saw that there is already a webVieClient member, but I don't if I have to use it and how.

I'm working with Phonegap version 1.9.0

Thanks for reading


Answer to Simon:

It worked this way, thanks!

public class MainActivity extends DroidGap {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    super.init();
    super.appView.clearCache(true);
    super.appView.clearHistory();
    this.appView.setWebViewClient(new CustomCordovaWebViewClient(this));
    super.loadUrl("file:///android_asset/www/index.html");
}

public class CustomCordovaWebViewClient extends CordovaWebViewClient {

 public CustomCordovaWebViewClient(DroidGap ctx) {
   super(ctx);
 }

 @Override
 public void onPageStarted(WebView view, String url, Bitmap bitmap) {
   super.onPageStarted(view, url, bitmap);
   Log.i("TEST", "onPageStarted: " + url);
 }

 @Override
 public void onPageFinished(WebView view, String url) {
   super.onPageFinished(view, url);
   Log.i("TEST", "onPageFinished: " + url);
 }

 @Override
 public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){  
     super.doUpdateVisitedHistory(view, url, isReload);  
 }

 @Override
 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     super.onReceivedError(view, errorCode, description, failingUrl);
 }

}

}
Spike777
  • 227
  • 5
  • 12

3 Answers3

7

I think I've figured this out on latest Cordova versions (I'm using 2.2). It fails at onPageStarted() because it's expecting an appView, which is null. Setting the appView seems to fix it eg

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();

    CordovaWebViewClient webViewClient = new CustomAcceptingWebViewClient(this);
    webViewClient.setWebView(this.appView);
    this.appView.setWebViewClient(webViewClient);

    super.loadUrl("file:///android_asset/www/index.html");

}

Note that the super.init() is also needed

janisz
  • 6,292
  • 4
  • 37
  • 70
antonylees
  • 106
  • 1
  • 4
5

To accomplish what you want to do I would extend the CordovaWebViewClient class and override the methods you want but don't forget to call the super methods or PhoneGap won't work without the CordovaWebViewClient as it is an important class.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Thanks for your answer Simon, I did that but it doesn't work, I updated the first post with the code implementing what you said, is that what you said? – Spike777 Jul 04 '12 at 14:49
  • It finally worked calling "super.onPageFinished(view, url);" in onPageFinished method... thanks! – Spike777 Jul 04 '12 at 15:35
  • Oh, I just figured that it only works in version 1.8.1, in 1.9.0 it doesn't work: it throws an error when calling super.onPageStarted(...) – Spike777 Jul 04 '12 at 15:46
  • Could you please advice or point a link to how to do the override the CordovaWebViewClient? I'm getting errors like "Webview cannot be resolved to a type" – arod Jun 09 '13 at 23:41
0

You forgot to call super ;)

    // Assign webclient.
    this.appView.setWebViewClient(new CordovaWebViewClient(me, this.appView) {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    });
E. Fortes
  • 1,338
  • 12
  • 12