Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio Android无法从api获取数据_Android Studio_Ionic Framework_Mobile_Fetch_Capacitor - Fatal编程技术网

Android studio Android无法从api获取数据

Android studio Android无法从api获取数据,android-studio,ionic-framework,mobile,fetch,capacitor,Android Studio,Ionic Framework,Mobile,Fetch,Capacitor,我正在尝试使用电容器将我的webapp移植到android。 除了一个部分,我已经搞定了 每当我想从数据库/服务器上获取一些东西时,我都在使用fetch。这在浏览器中非常有效——无论是在台式机还是移动设备上。 但当我通过Android Studio运行应用程序时,获取失败并显示此错误消息 E/电容器/控制台:文件:http://localhost/ -第0行-消息:未捕获(承诺中)类型错误:无法获取 在同一台设备上,但在浏览器中工作正常 我尝试添加一个network-security-confi

我正在尝试使用电容器将我的webapp移植到android。 除了一个部分,我已经搞定了

每当我想从数据库/服务器上获取一些东西时,我都在使用fetch。这在浏览器中非常有效——无论是在台式机还是移动设备上。 但当我通过Android Studio运行应用程序时,获取失败并显示此错误消息

E/电容器/控制台:文件:http://localhost/ -第0行-消息:未捕获(承诺中)类型错误:无法获取

在同一台设备上,但在浏览器中工作正常

我尝试添加一个network-security-config.xml,其中包含以下内容

<network-security-config>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">http://5e4ce8849526.ngrok.io</domain>
        </domain-config>
    </network-security-config>
</network-security-config>

我非常感谢您的帮助:)

检查此URL”“使用
HttpClient
angular功能,而不是调用时获取API。它在安卓、ios和浏览器中也能工作。您只需要在“app.module.ts”中的“imports:[HttpClientModule]”中添加
HttpClientModule
“我应该在帖子中提到这一点,但我不使用angular。我用的是本地javascript,很抱歉。尝试添加
让fetch:any
并检查。
let url = ' http://5e4ce8849526.ngrok.io'

function autoLogin(cb) {

    let key = localStorage.getItem('userToken');
    if(key == null || key == '') {
        
        return 'No key stored';
    
    } else {
    fetch(url + '/informatik/readingapp/restapi/api/users/login.php', {
            method: 'post',
            headers: {
                        'Authorization': 'Bearer ' + key
                    },
        })
        .then(res => res.json())
        .then((data) => {

            console.log(data)

            switch(data.message) {
                case 'Password not verified':
                    presentAlert('Forkert kodeord', 'Har du skrevet dit kodeord rigtigt?')
                    break;

                case 'Password verified':
                    nav.push('nav-home')

                    let tk = data.jwt.token
                    let usnm = data.username
                    let usid = data.id

                    cb(tk, usnm, usid);
                    break;

                case 'No login data': 
                    presentToastHome('Intet gemt login, desværre :(')
                    break;
            }
        })
    }
}