简体   繁体   中英

Capacitor Android not loading http javascript source

Im trying to load remote scripts / css from my local web server under url of http://127.0.0.1:4002/

But seem to be having error in capacitor android, capacitor ios is working fine. I also manage to load the css from without any error https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap-grid.min.css

My guess is not supporting http?

Here is my code in loading it

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap-grid.min.css" />
        <link href="http://127.0.0.1:4002/test/fonts.css" rel="stylesheet" />
        <meta charset="utf-8" />
        <meta
            name="viewport"
            content="initial-scale=1, maximum-scale=1.0, minimum-scale=1.0, width=device-width, height=device-height, viewport-fit=cover,  user-scalable=no"
        />
        <title>My Title</title>
    </head>

    <body id="body">
        
    </body>
</html>

Here is the error i got under 'chrome://inspect/#devices'在此处输入图像描述

My dependencies:

"@capacitor-community/http": "^1.0.0-alpha.1",
"@capacitor/android": "^3.0.0-rc.0",
"@capacitor/cli": "^3.0.0-rc.0",
"@capacitor/core": "^3.0.0-rc.0",
"@capacitor/device": "^0.5.5",
"@capacitor/dialog": "^0.4.5",
"@capacitor/filesystem": "^0.5.1",
"@capacitor/ios": "^3.0.0-rc.0",

My capacitor.config.json

{
    "appId": "com.example.app",
    "appName": "hello-cap-2",
    "bundledWebRuntime": false,
    "npmClient": "npm",
    "webDir": "www",
    "plugins": {
        "SplashScreen": {
            "launchShowDuration": 0
        }
    },
    "server": {
        "allowNavigation": ["*"]
    },
    "android": {
        "allowMixedContent": true
    },
    "cordova": {}
}

Got the answer at https://github.com/ionic-team/capacitor/discussions/4477

127.0.0.1 is your computer, iOS simulator understand it because the simulator is running on your computer, but on Android, the emulator is a "virtual machine" with its own networking, so 127.0.0.1 for it its the own emulator, not your computer.

You can enable port forwarding to send the 4002 port traffic to the emulator, or you can use 10.0.2.2 IP as it automatically loops to 127.0.0.1

https://developer.android.com/studio/run/emulator-networking

Also, note that using 127.0.0.1 won't work on real devices because of the same reason, it's better if you use the local IP of the computer instead, something like 192.168.1.30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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