I am trying to implement the workaround provided by Chris found here to allow a PhoneGap/Cordova-built Android app to make AJAX HTTPS requests to a server with a self-signed SSL certificate. I am using PhoneGap/Cordova 2.1.0, whereas Chris was using 1.7.0. I can create the MyWebViewClient class without issue. However, when I add this line of code…
this.setWebViewClient(this.appView, new MyWebViewClient(this));
…to the MainActivity class’ overridden init() method, I receive this error:
The method setWebViewClient(CordovaWebView, MyWebViewClient) is undefined for the type MainActivity
Here is my code for MyWebViewClient.java:
package [packagename];
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.DroidGap;
public class MyWebViewClient extends CordovaWebViewClient {
public class MyWebViewClient(DroidGap ctx) {
super(ctx);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
}
Here is my code for MainActivity.java:
package [packagename];
import android.os.Bundle;
import org.apache.cordova.*;
public class MainActivity extends DroidGap {
@Override
public void init() {
super.init();
this.setWebViewClient(this.appView, new MyWebViewClient(this)); // Error occurs here
}
@Override
public void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setBooleanProperty("keeprunning", false);
super.loadUrl("file:///android_asset/www/index.html");
}
}
I do not have enough reputation, otherwise I would have just commented on Chris’ answer. Also, I am not looking for a jQuery solution (I already know I can make a $.ajax() call to avoid this issue, but I am trying to keep jQuery out of my app).
Any ideas? Your help is greatly appreciated!
Edit: Please see my comments below before responding.
This can be fixed as below on later Cordova versions (I’m using 2.2). As mentioned, it fails at
onPageStarted()– this is because it’s expecting an appView, which is null so you get a NullPointerException. Setting the appView seems to fix it egNote that the
super.init()is also needed