Android Phonegap - TIMEOUT ERROR when trying to install WebViewClient

I work with Android and Phonegap, and currently I am having problems with one simple task. I need to configure webViewClient in the PhoneGap web browser to lock the page URL and work with it.

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

This code does not work, the page never loads, and I get a TIMEOUT ERROR, but if I delete the "setWebViewClient" part, the page will load perfectly.

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

  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 this code doesn't work either, I still have ERROR TIMEOUT. I also saw that there is already a webVieClient element, but I do not want to use it and how.

I work with Phonegap version 1.9.0

thank you for reading


Answer Simon:

It worked, 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); } } } 
+6
source share
3 answers

To accomplish what you want to do, I would extend the CordovaWebViewClient class and override the methods you want, but be sure to call the super methods or PhoneGap will not work without CordovaWebViewClient, since this is an important class.

+5
source

I think I figured it out on the latest versions of Cordoba (I'm using 2.2). It fails on onPageStarted() because it expects an appView that is null. Setting appView seems to fix it, e.g.

 @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 super.init() is also required

+7
source

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

Source: https://habr.com/ru/post/919619/


All Articles