phonegap 解决https访问问题

对于还没购买SSL证书的域名,在app端访通过https访问的时候是无法访问成功的,解决办法是在你程序的主activity里重写makeWebViewClient方法,返回我们自己定义的CordovaWebViewClient,在我们自己定义的CordovaWebViewClient 继承CordovaWebViewClient,然后重写onReceivedSslError方法,方法里直接“ handler.proceed();”,不处理错误,代码如下:

 

 

main.Java,友盟继承的可以不用看:

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public class main extends CordovaActivity   
  2. {  
  3.         
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         UmengUpdateAgent.setUpdateOnlyWifi(false);  
  9.         UmengUpdateAgent.update(this);  
  10.         super.init();  
  11.         // Set by <content src="index.html" /> in config.xml  
  12.         super.loadUrl(Config.getStartUrl());  
  13.         
  14.         //super.loadUrl("file:///android_asset/www/index.html")  
  15.     }  
  16.   
  17.     @Override  
  18.     protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) {  
  19.         // TODO Auto-generated method stub  
  20.          if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {  
  21.                 return new SSLAcceptingCordovaWebViewClient(this, webView);  
  22.             } else {  
  23.                 return new SSLAcceptingIceCreamCordovaWebViewClient(this, webView);  
  24.             }  
  25.     }  
  26. }  


SSLAcceptingCordovaWebViewClient:

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. import org.apache.cordova.CordovaInterface;  
  2. import org.apache.cordova.CordovaWebView;  
  3. import org.apache.cordova.CordovaWebViewClient;  
  4. import org.apache.cordova.DroidGap;  
  5.   
  6. import android.net.http.SslError;  
  7. import android.webkit.SslErrorHandler;  
  8. import android.webkit.WebView;  
  9.   
  10. public class SSLAcceptingCordovaWebViewClient extends CordovaWebViewClient{  
  11.       
  12.     public SSLAcceptingCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {  
  13.         super(cordova, view);  
  14.         // TODO Auto-generated constructor stub  
  15.     }  
  16.   
  17.         @Override  
  18.         public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {  
  19.             // testing against getPrimaryError() or hasErrors() will fail on Honeycomb or older.  
  20.             // You might check for something different, such as specific info in the certificate,  
  21.             //if (error.getPrimaryError() == SslError.SSL_IDMISMATCH) {  
  22.                 handler.proceed();  
  23.             //} else {  
  24.             //    super.onReceivedSslError(view, handler, error);  
  25.             //}  
  26.         }  
  27. }  


SSLAcceptingIceCreamCordovaWebViewClient.java,名字有点长,网上参考下来的,也懒得去改了:

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. import org.apache.cordova.CordovaInterface;  
  2. import org.apache.cordova.CordovaWebView;  
  3. import org.apache.cordova.IceCreamCordovaWebViewClient;  
  4.   
  5. import android.net.http.SslError;  
  6. import android.webkit.SslErrorHandler;  
  7. import android.webkit.WebView;  
  8.   
  9. public class SSLAcceptingIceCreamCordovaWebViewClient extends IceCreamCordovaWebViewClient {  
  10.     public SSLAcceptingIceCreamCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {  
  11.         super(cordova, view);  
  12.     }  
  13.   
  14.     @Override  
  15.     public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {  
  16.         handler.proceed();  
  17.     }  
  18. }  

猜你喜欢

转载自lishuaishuai.iteye.com/blog/2302226