Failed call api http in android - android

i am using API localhost in android but can not call it from android. I have change localhost became IP but can not call it too. I have been using another API and it is success. I think it is because of http connection.

In the end i am using this reference. I also get some answer from this.
You need to define a res/xml/network_security_config.xml and permit HTTP for that host:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">server.com</domain>
</domain-config>
</network-security-config>
That network_security_config.xml also needs to be referenced in the Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
...>
<application
android:networkSecurityConfig="#xml/network_security_config"
...>
...
</application>
</manifest>

Related

HTTP Links Not Opening in Android WebView in Android Studio Java?

When I open a URL that Starts with "https://" in android WebView it works properly and Play a Video in video player from same type of URL ("https://") video also plays well.
But whenever I tries to open non secure ("http://") URL WebView and Video Player both don't work. If you have any solution of this question then please answer ?
Starting with Android 9 (API level 28), cleartext support is disabled by default.
You need to set Manifest and res/xml/network_security_config.xml properly. Below example is for http://test.com:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="#xml/network_security_config"
... >
...
</application>
</manifest>
res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="false" />
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">test.com</domain>
</domain-config>
</network-security-config>

Cleartext HTTP traffic to ... not permitted

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>

Allow HTTP (ClearText) from an IP address | Flutter 2 | Android

I need to get a JSON response from an IP address (No Domain Name connected), but flutter 2.0 doesn't allow HTTP, so I followed this answer to enable it, but the problem I face is I dont have domain name, to replace here:
<?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>
I tried replacing with IP but it throws error that IP is not recognised as domain name.
<domain includeSubdomains="true">x.x.x.x</domain> #doesn't work
Any solution to allow HTTP traffic from IP address.
PS: I know its unsafe.
EDIT:
I tried this methods:
Setting res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Doesn't work.
Setting res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">65.0.XX.136</domain>
</domain-config>
</network-security-config>
Doesn't Work.
Setting android:usesCleartextTraffic="true" in application tag of AndroidManifest.xml.
Doesn't work.

networkSecurityConfig not working for http connection

I'm testing http connection with apache server on ec2 and my android app.
Connection works fine on emulator(API 24) but it won't work on my Samsung Galaxy S10(API 29).
Tried everything related to configuring network_security_config.xml but nothing worked.
In my build.gradle minSdkVersion is 16 and targetSdkversino is 29. Below are my trials. (Let's assume i'm trying to connect to IP address of 1.2.3.4)
1) Adding custom network_security_config.xml
in res/xml/network_security_config.xml,
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">
1.2.3.4
</domain>
</domain-config>
</network-security-config>
in my manifest,
<application
android:networkSecurityConfig="#xml/network_security_config"
2) Setting usesClearTextTraffic as true
in my manifest,
<application
android:usesCleartextTraffic="true"
3) Applying both 1) and 2)
in my manifest,
<application
android:usesCleartextTraffic="true"
android:networkSecurityConfig="#xml/network_security_config"
4) Setting tools:targetApi with 3)
in my manifest,
<application
android:usesCleartextTraffic="true"
android:networkSecurityConfig="#xml/network_security_config"
tools:targetApi="29"
5) using base-config instead of domain-config
in res/xml/network_security_config.xml,
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
I have no idea what's wrong here.

How to fix cleartext traffic not permitted issue in Xiomi Redmi 3S with Android 6.0.1

When i am using URL with http then it shows an error "CLEARTEXT communication to (domain name) not permitted by network security policy" . I have tried cleartextTrafficPermitted = true in manifest.xml file but it is not compatible with payment gateway i use, which need cleartextTrafficPermitted = false. As it is API level 23 so, networkSecurityConfig = "#xml/network_security_config" doesn't work in it. I am using Retrofit as networking Library.
It is device specific issue Xiomi Redmi 3S, MIUI Global 10.2, Android version 6.0.1.
Any suggestion about this issue.
Finally i got solution by medium post , Code i used :
Step 1: create file : res/xml/network_security_config.xml
Add code in this file:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain>
</domain-config>
</network-security-config>
Step 2: Add line in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="#xml/network_security_config"
...>
...
</application>
</manifest>

Categories

Resources