cordova应用在遇到https下安全证书有问题的解决方式

package com.aviva_cofco.crowdfunding.ssl;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;

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);
        // }
    }
}
package com.aviva_cofco.crowdfunding.ssl;

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();
    }
}
/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package com.aviva_cofco.crowdfunding;

import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.aviva_cofco.crowdfunding.ssl.SSLAcceptingCordovaWebViewClient;
import com.aviva_cofco.crowdfunding.ssl.SSLAcceptingIceCreamCordovaWebViewClient;

public class CordovaApp extends CordovaActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.init();
        // super.i
        setOriention();
        /*
         * appView.setWebViewClient(new WebViewClient(){
         * 
         * @Override public void onReceivedSslError(WebView view,
         * SslErrorHandler handler, SslError error) { // TODO Auto-generated
         * method stub handler.proceed(); } });
         * appView.getSettings().setDomStorageEnabled(true);
         */
        // appView.setWebContentsDebuggingEnabled(true);
        // 修改浏览器UA
        String ua = appView.getSettings().getUserAgentString();
        // 加入当前App的版本号
        ua += "&=AppVersion:0.1";
        appView.getSettings().setUserAgentString(ua);

        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setOriention();
    }

    private void setOriention() {
        if (isPad() || isCustomerDevice()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 判断是否是某些特殊设备
     * 
     * @return
     */
    private boolean isCustomerDevice() {
        String model = Build.MODEL;
        if ("N5100".equals(model)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean isPad() {
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);
        // 屏幕宽度
        float screenWidth = p.x;
        float screenHeight = p.y;
        screenWidth = Math.max(screenWidth, screenHeight);
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        int cssWidth = (int) (screenWidth / dm.density + 0.5f);

        if (cssWidth > 700) {
            return true;
        }

        return false;
    }

    @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);
        }
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值