问题描述
阿贾克斯HTTPS从我在Android上的PhoneGap /科尔多瓦App请求莫名其妙地失败,状态= 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商店或亚洲开发银行
实用的应用程序。我presume它可以使用证书进行连接,因为不是所有的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
就足够了 - 它会自动绕过证书错误
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.
不过,谷歌Play商店将不会接受一个APK与机器人:可调试=真正的
。首先,证书,当然,需要固定。不过,虽然出现这种情况,这里是一个解决方法的PhoneGap /科尔多瓦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:
-
在你的应用程序包创建一个子类
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();
}
}
在&lt;您的应用程序名称&gt;的.java
添加替代的 makeWebViewClient
:
in <Your App Name>.java
add an override for 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应用Ajax请求到HTTPS失败,状态0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!