简体   繁体   English

Phonegap 应用程序可用于预览但不适用于 Android 设备 - Promise

[英]Phonegap app working on preview but not on Android device - Promise

I have a Phonegap app which runs perfectly on the preview app but when I create an APK using Phonegap Build and install it on an Android device it works partially excepting any code that is inside a Promise.我有一个 Phonegap 应用程序,它可以在预览应用程序上完美运行,但是当我使用 Phonegap Build 创建一个 APK 并将其安装在 Android 设备上时,它部分工作,除了 Promise 内的任何代码。 I added a few alerts and I noticed it stops working right before a Promise.我添加了一些警报,我注意到它在 Promise 之前停止工作。

I have the following code:我有以下代码:

    {
        alert('passwordreset before promise');
        return new Promise((resolve, reject) => {
            alert('inside promise');
            this.api.call( '/' + this.endpoint + '/recover', {email: email})
            .then(response => 
            { alert('promise succes');
                resolve(response);
            })
            .catch(errors => 
            {
                reject(errors);
            });

        });
    } 

The first alert (password before promise) is executed but not the others.第一个警报(promise 之前的密码)被执行,但不执行其他警报。

Do you have any idea what might be going on?你知道会发生什么吗?

Do you use Android studio?你使用安卓工作室吗? If so check what log do you have in LogCat tab.如果是这样,请检查您在 LogCat 选项卡中有什么日志。 Did you set proper CORS settings?您是否设置了正确的 CORS 设置? It looks like you are making some api requests.看起来您正在发出一些 api 请求。 In MSDN there's a nice article describing what settings you may be missing: https://docs.microsoft.com/en-us/visualstudio/cross-platform/tools-for-cordova/security/whitelists?view=toolsforcordova-2017在 MSDN 中有一篇很好的文章描述了您可能缺少哪些设置: https : //docs.microsoft.com/en-us/visualstudio/cross-platform/tools-for-cordova/security/whitelists?view=toolsforcordova-2017

Since Android 9 clear text is disabled by default.由于默认情况下禁用 Android 9 明文。 If the problem still persist verify your network_security_settings.xml (if it's set).如果问题仍然存在,请验证您的 network_security_settings.xml(如果已设置)。

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
  <domain includeSubdomains="true">10.0.1.1</domain> <!-- Set local api -->
</network-security-config>

You can set network settings file in your Android Manifest您可以在 Android Manifest 中设置网络设置文件

<?xml version="1.0" encoding="utf-8"?>
 <manifest>
 <application android:networkSecurityConfig="@xml/network_security_config">
    ...
 </application>
 </manifest>

Promises are not supported in webview. webview 不支持承诺。 You need to transpile/compile your JS code for ES5.您需要为 ES5 转译/编译您的 JS 代码。

Promises that are part of the ES2015 JavaScript specification (also referred to as ES6). Promise 是 ES2015 JavaScript 规范(也称为 ES6)的一部分。

If you need to polyfill, check out https://www.promisejs.org/如果您需要 polyfill,请查看https://www.promisejs.org/

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

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