Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Phonegap - TIMEOUT ERROR when trying to set a WebViewClient

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);
 }

}

}
like image 843
Spike777 Avatar asked Jul 03 '12 19:07

Spike777


1 Answers

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

like image 192
antonylees Avatar answered Oct 29 '22 20:10

antonylees