简体   繁体   English

Angular 和 Ionic,$http 不适用于已签名的 .apk

[英]Angular & Ionic, $http not working on signed .apk

I have an ionic v1 application which was working fine, but a few weeks ago when I built new signed .apk file all $http requests just stop working.我有一个运行良好的 ionic v1 应用程序,但几周前当我构建新的签名 .apk 文件时,所有 $http 请求都停止工作。

Here is an example of request这是请求的示例

$http({
      method: 'GET',
      url: 'https://some-url.com/user/id'
    }).then(function (response) {
        alert(response.data);
      }, function (error) {
      alert('Error ' + JSON.stringify(error));
});

I always get error callback with {data: null, status -1, headers: ...}我总是收到错误回调 {data: null, status -1, headers: ...}

What is interesting that when I build simple apk or just running cordova run android everything works fine.有趣的是,当我构建简单的 apk 或仅运行cordova run android一切正常。 Only not working in signed apk.只是不能在签名的 apk 中工作。

I already tried to update cordova-plugin-whitelist and added我已经尝试更新cordova-plugin-whitelist并添加

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

to my index.html file.到我的 index.html 文件。 Also added <allow-navigation href="*" /> to my config.xml file.还将<allow-navigation href="*" />到我的 config.xml 文件中。

Does anyone have some ideas why this is happening?有没有人有一些想法为什么会发生这种情况? Maybe android sdk changed the way of building signed apk and I need some addintional configuration.也许 android sdk 改变了构建签名 apk 的方式,我需要一些额外的配置。

You can ignore certificate by changing the following code on the CordovaWebViewClient.java file you can find the java file in Cordova version 4 or less platforms/android/CordovaLib/src/org/apache/cordova/CordovaWebViewClient.java您可以通过更改 CordovaWebViewClient.java 文件中的以下代码来忽略证书,您可以在 Cordova 版本 4 或更低版本中找到该 java 文件 platform/android/CordovaLib/src/org/apache/cordova/CordovaWebViewClient.java

or Cordova version 5 or greater platforms/android/CordovaLib/src/org/apache/cordova/engine/SystemWebViewClient.java或 Cordova 版本 5 或更高版本平台/android/CordovaLib/src/org/apache/cordova/engine/SystemWebViewClient.java

public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
  final String packageName = this.cordova.getActivity().getPackageName();
  final PackageManager pm = this.cordova.getActivity().getPackageManager();

  ApplicationInfo appInfo;
  try {
    appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
      // debug = true
      handler.proceed();
      return;
    } else {
      // debug = false
      // THIS IS WHAT YOU NEED TO CHANGE:
      // 1. COMMENT THIS LINE
      // super.onReceivedSslError(view, handler, error);
      // 2. ADD THESE TWO LINES
      // ---->
      handler.proceed();
      return;
      // <----
    }
  } catch (NameNotFoundException e) {
    // When it doubt, lock it out!
    super.onReceivedSslError(view, handler, error);
  }
}

Hope this solution will work for you.希望这个解决方案对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM