4

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.

Community
  • 1
  • 1
Jesse
  • 608
  • 8
  • 19
  • I changed the problematic line of code to "this.webViewClient = new MyWebViewClient(this);" and I can build the app and still make AJAX HTTP requests, but for AJAX HTTPS requests to a server with a self-signed SSL certificate, I still get HTTP status code 0 back. – Jesse Oct 25 '12 at 16:29
  • I also changed the problematic line of code to "this.appView.setWebViewClient(new MyWebViewClient(this));" but I get a fatal exception when I start the app. *Sigh* – Jesse Oct 25 '12 at 16:42
  • I found [this post](http://stackoverflow.com/questions/11317813/android-phonegap-timeout-error-when-trying-to-set-a-webviewclient) and testing the exact same code in the answer to Simon using Cordova/PhoneGap 2.1.0 also results in an error when calling super.onPageStarted(...). Hence, I suppose my problem lies in the fact that the approach I am taking simply doesn't work in Cordova/PhoneGap 2.1.0. Still, I assume there must be a way to override the onReceivedSslError(...) method of the CordovaWebViewClient class in Cordova/PhoneGap 2.1.0. Any fresh ideas? – Jesse Oct 25 '12 at 19:05
  • It seems to simple to be the answer but could you try super.setWebViewClient instead of using this? Edit: Just realized this question is a month old but if you've still got the problem I'm still curious to see if its the answer – Nick Roth Nov 30 '12 at 03:09

1 Answers1

2

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 eg

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

    CordovaWebViewClient webViewClient = new CustomWebViewClient(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

antonylees
  • 106
  • 1
  • 4
  • Thank you! Luckily the Cordova release cycle has been short lately, and I am now using version 2.2 also. Much appreciated. – Jesse Dec 24 '12 at 15:20