I built an ionic/angular web app via ionic build, then created the android project with capacitor via "ionic capacitor run android -l --external". I'm trying to make an http post request to a local web api. Api's IP is 127.0.0.1:43308, while the android project is running at http://192.168.1.6:8100. I tried many solutions online and all led me back to the same error
net::ERR_CONNECTION_REFUSED
Anyone got ideas? the project has no issues when ran through a web browser, only gives me such error when it's ran as a native application
Capacitor.config.json:
{
"appId": "com.reservation.app",
"appName": "reservation-app",
"webDir": "www",
"npmClient": "npm",
"server": {
"url": "http://192.168.1.6:8100",
"cleartext": true,
"allowNavigation":[
"localhost:8100/*"
]
}
}
Config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<access origin="*" />
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:networkSecurityConfig="@xml/network_security_config" />
<application android:usesCleartextTraffic="true" />
</edit-config>
<preference name="ScrollEnabled" value="false" />
<preference name="android-minSdkVersion" value="19" />
<preference name="BackupWebStorage" value="none" />
<preference name="SplashMaintainAspectRatio" value="true" />
<preference name="FadeSplashScreenDuration" value="300" />
<preference name="SplashShowOnlyFirstTime" value="false" />
<preference name="SplashScreen" value="screen" />
<preference name="SplashScreenDelay" value="3000" />
<feature name="CordovaHttpPlugin">
<param name="android-package" value="com.silkimen.cordovahttp.CordovaHttpPlugin"/>
</feature>
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils"/>
<param name="onload" value="true"/>
</feature>
<feature name="Whitelist">
<param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin"/>
<param name="onload" value="true"/>
</feature>
</widget>
EDIT: Web API's CORs policy:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: AllowSpecificOrigins,
builder =>
{
builder.WithOrigins( "http://localhost:8100", "http://192.168.1.6:8100")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors(AllowSpecificOrigins);
}