对于还没购买SSL证书的域名,在app端访通过https访问的时候是无法访问成功的,解决办法是在你程序的主activity里重写makeWebViewClient方法,返回我们自己定义的CordovaWebViewClient,在我们自己定义的CordovaWebViewClient 继承CordovaWebViewClient,然后重写onReceivedSslError方法,方法里直接“ handler.proceed();”,不处理错误,代码如下:
main.java,友盟继承的可以不用看:
public class main extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
UmengUpdateAgent.setUpdateOnlyWifi(false);
UmengUpdateAgent.update(this);
super.init();
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html")
}
@Override
protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) {
// TODO Auto-generated method stub
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
return new SSLAcceptingCordovaWebViewClient(this, webView);
} else {
return new SSLAcceptingIceCreamCordovaWebViewClient(this, webView);
}
}
}
SSLAcceptingCordovaWebViewClient:
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.DroidGap;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
public class SSLAcceptingCordovaWebViewClient extends CordovaWebViewClient{
public SSLAcceptingCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
// TODO Auto-generated constructor stub
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// testing against getPrimaryError() or hasErrors() will fail on Honeycomb or older.
// You might check for something different, such as specific info in the certificate,
//if (error.getPrimaryError() == SslError.SSL_IDMISMATCH) {
handler.proceed();
//} else {
// super.onReceivedSslError(view, handler, error);
//}
}
}
SSLAcceptingIceCreamCordovaWebViewClient.java,名字有点长,网上参考下来的,也懒得去改了:
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.IceCreamCordovaWebViewClient;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
public class SSLAcceptingIceCreamCordovaWebViewClient extends IceCreamCordovaWebViewClient {
public SSLAcceptingIceCreamCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
}