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);
}
}
}
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
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.
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);
}
});
Related
i am trying load a .php file in WebView. it was working properly before API level 28 after i updated to API level 28 its not working its showing white screen. nothing is showing i tried all the options.
Here is the code
I am added the safe browsing false in the manifest file also.
I tried searching in the google nothing is helped me if any one done please help in this case to resolve.
String url="https://xxx/xxx/abc.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleSSLHandshake();
WebView mWebView = (WebView) findViewById(R.id.webView);
// PackageInfo webViewPackageInfo = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// webViewPackageInfo = WebView.getCurrentWebViewPackage();
mWebView.getSettings().setSafeBrowsingEnabled(false);
// Log.d("MY_APP_TAG", "WebView version: " + webViewPackageInfo.versionName);
}
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
mWebView.setWebViewClient(webViewClient);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
}
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
// Log.i(TAG,url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
}
I solved my problem. it was the certificates issues, i just added the following code, its working fine now
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
I have written custom webviewclient class to override onPageStarted, onPageFinished etc in cordova 3.7 which was working fine.
In following code is I have hosted the www directory to web server and interacting cordova plugins from there (barcodescanner, nfc, bluetooth etc).
public class MainActivity extends CordovaActivity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
loadUrl("https://example.com");
}
public class CustomCordovaWebViewClient extends CordovaWebViewClient {
public CustomCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
}
}
After a year, I have migrated project from cordova 3.7 to cordova 6 but I found above code broken like CordovaWebViewClient, super.onPageStarted etc can't resolve symbols. I also tried CordovaWebViewImpl and confused myself.
After searching alot on google I found solution which were mostly given in 2011-14 which are not applicable. I couldn't found cordova docs helpful.
It was replaced by SystemWebViewClient
You should do something like this:
SystemWebView wv = (SystemWebView)appView.getView();
wv.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)appView.getEngine()){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
});
Cordova 4 removed the CordovaWebViewClient : look here
You may use WebViewClient instead of CordovaWebViewClient (The cordova-plugin-inappbrowser plugin use that for override onPageStarted event).
public class CustomCordovaWebViewClient extends WebViewClient
Apache cordova removed CordovaWebViewClient inorder to support external webviews like Crosswalk. If you check out the 14 changed files link in the following commit link, you could see CordovaWebViewClient is removed and AndroidWebViewClient is added.
So i guess you cannot use the same old code work in Cordova 6.0
You can probably try using org.apache.cordova.engine.SystemWebViewClient instead.
Infact, the same question is answered here and it was also accepted. So i believe this is the possible solution to the issue. Hope it helps.
I have written custom webviewclient class to override onPageStarted, onPageFinished etc in cordova 3.7 which was working fine.
In following code is I have hosted the www directory to web server and interacting cordova plugins from there (barcodescanner, nfc, bluetooth etc).
public class MainActivity extends CordovaActivity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
loadUrl("https://example.com");
}
public class CustomCordovaWebViewClient extends CordovaWebViewClient {
public CustomCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
}
}
After a year, I have migrated project from cordova 3.7 to cordova 6 but I found above code broken like CordovaWebViewClient, super.onPageStarted etc can't resolve symbols. I also tried CordovaWebViewImpl and confused myself.
After searching alot on google I found solution which were mostly given in 2011-14 which are not applicable. I couldn't found cordova docs helpful.
It was replaced by SystemWebViewClient
You should do something like this:
SystemWebView wv = (SystemWebView)appView.getView();
wv.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)appView.getEngine()){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
});
Cordova 4 removed the CordovaWebViewClient : look here
You may use WebViewClient instead of CordovaWebViewClient (The cordova-plugin-inappbrowser plugin use that for override onPageStarted event).
public class CustomCordovaWebViewClient extends WebViewClient
Apache cordova removed CordovaWebViewClient inorder to support external webviews like Crosswalk. If you check out the 14 changed files link in the following commit link, you could see CordovaWebViewClient is removed and AndroidWebViewClient is added.
So i guess you cannot use the same old code work in Cordova 6.0
You can probably try using org.apache.cordova.engine.SystemWebViewClient instead.
Infact, the same question is answered here and it was also accepted. So i believe this is the possible solution to the issue. Hope it helps.
In iPhone we have functions like webViewDidStartLoad and webViewDidFinishLoad to check the Start of loading and finish of particular url.
Do we have anythng like this on Android ?
You could try something like this :
webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Do something here
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Do something here
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// Do something here
}
});
Yes you can use WebViewClient :
private class CustomWebClient extends WebViewClient{
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
}
public void onPageFinished(WebView view, String url)
{
}
}
Usage:
webView.setWebViewClient(new CustomWebClient());
Every method name is self explanatory and you can also check this : http://developer.android.com/reference/android/webkit/WebViewClient.html
Is there a way to set the timeout value in WebView?
I want the WebView to be timeouted if the url is too slow to response.
You can do it by setting up a Timer which checks for progress of current page by calling getProgress() and if it is less than some threshold after some specified time then you can dismiss the loading of the current page.
We can use onLoadResource method of WebViewClient instead of Timer. Like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WEBCLIENT", "onPageFinished");
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
Log.d("WEBCLIENT","onLoadResource");
if(webView.getProgress() == 100) {
progressDialog.dismiss();
}
}
}
I use
#Override
public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.alert_dialog);
Button btTryAgain = dialog.findViewById(R.id.bt_try_again);
btTryAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
recreate();
}
});
dialog.show();
//Toast with error conection
Toast.makeText(getApplicationContext(), "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
}
Where -alert_dialog- is a layout with a button to retry