[Android] Cleartext HTTP traffic to X.X.X not permitted
Goal
- Solve ‘Cleartext HTTP traffic to X.X.X not permitted’ error
Problem
- I replaced the target SDK 27 with 28(Pie 9.0)
- After that, when trying to communicate with server by HTTP protocol, this error occured

Solution
Cause
- From API level 28(Pie 9.0) it has been default to prevent unencrypted HTTP protocl

1. Add a Network Security Configuration file
- The Network Security Configuration feature uses an XML file where you specify the settings for your app
1.1 Add xml file in res/xml
- Create network_security_config.xml file in src/res/xml directory


1.2. Add options
- Recommend to allow only specific domains
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="utf-8"?> <network-security-config> <!-- Allow specific domains --> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">iot.qubics.kr</domain> </domain-config> <!-- Allow all domains --> <!-- <base-config cleartextTrafficPermitted="true" /> --> </network-security-config> |
1.3. Edit AndroidManifest.xml
- Add android:networkSecurityConfig=”@xml/network_security_config” option
1 2 3 4 5 6 7 8 9 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="io.dorbae.android"> <application android:networkSecurityConfig="@xml/network_security_config" ...> .... </application> </manifest> |

2. Edit only AndroidManiofest.xml
- Add android:useCleartextTraffic=”true” option
div style=”background: #eeeedd; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;”><table><tr><td><pre style="margin: 0; line-height: 125%">1 2 3 4 5 6 7 8 9</pre></td><td><pre style="margin: 0; line-height: 125%"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="io.dorbae.android"> <application android:usesCleartextTraffic="true" …> …. </application> </manifest> </pre></td></tr></table></div>

댓글남기기