I tried make a Http request on Android 24 using code like
url = "http://10.0.2.2:8080/api/";
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
conn.setConnectTimeout(3000);
conn.setReadTimeout(5000);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.connect();
First I got an error like
java.io.IOException: Cleartext HTTP traffic to 10.0.2.2 not permitted
Then I added android:usesCleartextTraffic="true" to AndroidManifest.xml as some solutions pointed out,
Now I get an error like
java.net.ConnectException: Failed to connect to /10.0.2.2:8080
The error gives no much useful information, I don't know how to search and solve this error.
The server should be OK, for the code can pass on Android 23.
First check did you added network permission in your manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Just add the below code to your manifest file.
android:networkSecurityConfig="#xml/network_config"
Before you need to create a network rule
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">someDomain.com</domain>
</domain-config>
</network-security-config>
Make sure you have added network permission in manifest
Have you tried this server in your browser? Address 10.0.2.2 points to your machine's localhost. If you open http://localhost:8080
(on computer runnig the emulator) you may get more info about the error. It's worth mentioning, that next to clearTraffic manifest flag, you now need to specify Network security configuration as well:
<application
....
android:networkSecurityConfig="#xml/network_rules"
....
Network rules xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">someDomain.com</domain>
</domain-config>
</network-security-config>
Looks like you are using http request. You can use https request to avoid this issue.Try with this for http request.
Put the below line in android manifest under application tag
android:networkSecurityConfig="#xml/network_security_config"
Create xml file inside res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">your domain</domain>
</domain-config>
</network-security-config>
Related
I'm getting a Network request failed only on android simulator.
ApolloError: Network request failed
at new ApolloError (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=myapp&modulesOnly=false&runModule=true:162987:26)
at Object.error (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app= myapp&modulesOnly=false&runModule=true:140360:70)
at notifySubscription (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app= myapp&modulesOnly=false&runModule=true:158462:20)
It works fine on iOS and iOS simulator.
The call is a real production server and not one hosted on my machine.
Already tried setting up android:usesCleartextTraffic="true" in the manifest.
Also whitelisted my domain in a android/app/src/main/res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">https://myserverdomain.com</domain>
<domain includeSubdomains="true">127.0.0.1</domain>
<domain includeSubdomains="true">10.0.0.1</domain>
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
what am I missing here ?
Solved!
First thing to know, if using Axios, it adds headers to the request.
Also, my android .env variable where was stored the url didn't work.
I am simply trying to make a Capacitor (Angular) HTTP GET Request from the Android Virtual Device (API 29) to an API (.NET 6.0) also running on my PC.
In production I would change the API to only use HTTPS but for development I wanted HTTP as I wouldn't expect the AVD to accept the self-signed certificate.
The error I see is simply
was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://10.0.2.2:5409/FromMobile/test'. This request has been blocked; the content must be served over HTTPS.
I assume this is an issue mainly because Capacitor is serving via HTTPS but the call is to a non-secured HTTP api.
I tried with my IP address too but had the same error.
I have already added
android:usesCleartextTraffic="true"
to the manifest.xml file.
I also tried adding the network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
and
android:networkSecurityConfig="#xml/network_security_config"
But no change
UPDATE:
I have also tried installing the certificate on to the phone but this had made no difference
Try to change the Capacitor config:
const config: CapacitorConfig = {
server: {
androidScheme: 'http',
cleartext: true
},
android: {
allowMixedContent: true
},
};
Use this network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
my program takes an URL from the user, so it may make request to any website of the internet.
I'm trying to make this possible, I looked up all the answers about "Android HTTP Cleartext" errors, and made this, but it still doesn't let me connect my test local PHP server, what am I missing here?
<uses-permission android:name="android.permission.INTERNET" />
...
<application
...
android:usesCleartextTraffic="true"
android:networkSecurityConfig="#xml/network_security_config"
tools:ignore="UnusedAttribute"
My security config:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">api.example.com</domain>
</domain-config>
</network-security-config>
Thanks!
Try changing your network_security_config.xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true"></base-config>
</network-security-config>
I am using a http://something API for the login process. But could't get a hit on API in Android 10. For rest of the version, the API is working fine.
First of all, I was getting SocketTimeoutException. Then I tried following solutions.
1) Added below attribute to <application> in Manifest.
android:usesCleartextTraffic="true"
Result: Still getting SocketTimeoutException.
2) Then I added networkSecurityConfig:
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">project.dev.company:6001/</domain>
<trust-anchors>
<certificates src="system"/>
</trust-anchors>
</domain-config>
</network-security-config>
Result : UnknownServiceException : CLEARTEXT communication to project.dev.company:6001/ is not permitted by network security policy.
3) Also tried permitting CLEARTEXT in <base-config>. Still getting SocketTimeoutException.
How can I permit my app to access a HTTP connection from Android 10? I am using Retrofit2 for network calling.
Use this codes in your xml file
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">project.dev.company</domain>
</domain-config>
</network-security-config>
and use this codes in manifest:
android:usesCleartextTraffic="true"
android:networkSecurityConfig="#xml/network_security_configuration"
dont need any "/" and port in domain,just use like me
Edited:
you can use IP of your api host,like this:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.1.1</domain>
</domain-config>
</network-security-config>
I cannot get rid of Cleartext HTTP traffic error in my Nativescript application. I saw all the relevant threads and added the following configuration to my app:
In AndroidManifest.xml added:
<application
android:usesCleartextTraffic="true"
android:networkSecurityConfig="#xml/network_security_config">
Also added the following file in to two locations:
App_Resources/Android/src/main/res/xml/network_security_config.xml
App_Resources/Android/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">ec2-xxx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com</domain>
</domain-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
And the error:
{"originalStack":"Error: java.io.IOException: Cleartext HTTP traffic to ec2-xxx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com not permitted\n at new ZoneAwareError (file:///data/data/com.app.app/files/app/vendor.js:92106:33)\n at onRequestComplete (file:///data/data/com.app.app/files/app/vendor.js:131598:34)\n at Object.onComplete (file:///data/data/com.app.app/files/app/vendor.js:131587:13)","zoneAwareStack":"Error: java.io.IOException: Cleartext HTTP traffic to ec2-xxx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com not permitted\n at onRequestComplete (file:///data/data/com.app.app/files/app/vendor.js:131598:34) []\n at Object.onComplete file:///node_modules\tns-core-modules\http\http-request\http-request.js:43:0
Running on Android Emulator-Pixel_2_API_28:5554
UPDATE:
It works on real device.