Phonegap Android 应用程序对 HTTPS 的 ajax 请求失败,状态为 0 [英] Phonegap Android app ajax requests to HTTPS fail with status 0

查看:28
本文介绍了Phonegap Android 应用程序对 HTTPS 的 ajax 请求失败,状态为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自我在 Android 上的 PhoneGap/Cordova 应用程序的 Ajax HTTPS 请求莫名其妙地失败,状态 = 0.它仅在使用发布密钥签署应用程序时出现(即从 ADT 导出),但在使用调试密钥签名时不出现(直接在模拟器或手机中运行).

Ajax HTTPS requests from my PhoneGap/Cordova app on Android inexplicably fail with status=0. It appears only when signing the app with the release key (i.e., exporting from ADT), but doesn't appear when signing with debug key (running directly in emulator or phone).

request = new XMLHttpRequest()
request.open "GET", "https://some.domain/", true
request.onreadystatechange = ->
  console.log "** state = " + request.readyState
  if request.readyState is 4
      console.log "** status = " + request.status

request.send()

总是输出

** state = 4
** status = 0

我是从 Play 商店安装应用程序还是使用 adb 实用程序安装该应用程序都没有关系.我认为它可以与证书相关联,因为并非所有 HTTPS 域都以这种方式失败.

It doesn't matter if i install the app from Play Store or with adb utility. I presume it could be connected with the certificate, since not all HTTPS domains fail this way.

推荐答案

当请求的 URL 响应错误或自签名证书时会发生这种情况.在测试或将应用分发给朋友时,在 AndroidManifest.xml 中设置 <application android:debuggable="true"...> 就足够了——它会自动绕过证书错误.

It happens when the requested URL responds with an erroneous or self-signed certificate. While testing or distributing the app to friends, setting <application android:debuggable="true"...> in AndroidManifest.xml is enough — it automatically bypasses certificate errors.

但 Google Play 商店不接受带有 android:debuggable="true" 的 APK.首先,证书当然需要固定.但是,当发生这种情况时,以下是 PhoneGap/Cordova 3 的解决方法:

But Google Play Store will not accept an APK with android:debuggable="true". First of all, the certificates, of course, need to be fixed. But while that happens, here is a workaround for PhoneGap/Cordova 3:

  1. 在您的应用程序包中为 CordovaWebViewClient 创建一个子类:

public class SSLAcceptingCordovaWebViewClient extends CordovaWebViewClient {
    public SSLAcceptingCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
        super(cordova, view);
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
}

  • IceCreamCordovaWebViewClient 相同:

    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();
        }
    }
    

  • .java 中为 makeWebViewClient 添加覆盖:

    @Override
    protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) {
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
            return new SSLAcceptingCordovaWebViewClient(this, webView);
        } else {
            return new SSLAcceptingIceCreamCordovaWebViewClient(this, webView);
        }
    }
    

  • 等等! SSL 错误将被忽略.但是,永远不要使用错误的证书.尝试先修复它们,并仅在您用完其他解决方案时才使用这种肮脏的解决方法.

    Et voilà! SSL errors will be disregarded. However, never use erroneous certificates. Try to fix them first and use this dirty workaround only when you run out of other solutions.

    这篇关于Phonegap Android 应用程序对 HTTPS 的 ajax 请求失败,状态为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆