Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

끈기 있는 개발 공간

Cleartext HTTP traffic to ... not permitted 예외 해결 본문

Trial and error

Cleartext HTTP traffic to ... not permitted 예외 해결

tenacy 2021. 8. 9. 10:22

예외

 

원인

안드로이드에서 기본적으로 Http 접근을 허용하지 않기 때문에 발생하는 에러입니다. Https로 접근하면 문제 없지만 해당 사이트가 Https를 지원하지 않아 예외 처리를 해야 합니다.

 

해결

안드로이드 공식 문서의 일반 텍스트 트래픽 선택 해제를 보시면 Android 9(API 수준 28)부터는 일반 텍스트 지원이 기본적으로 사용되지 않는다고 합니다. 따라서 API 28 이후에서 Http에 접근하려면 cleartext Http를 활성화시켜야 합니다.

 

모든 Http URL에 대해서 접근 허용

<application
        android:label="@string/app_name"
        ...
        android:usesCleartextTraffic="true">

 

일부 Http URL에 대해서 접근 허용

android:usesCleartextTraffic은 모든 Http 사이트에 대한 접근을 허용합니다. 만약 몇몇 사이트에 대한 접근만 허용하려면 /res/xml/network_security_config.xml 파일을 생성하고 예외 항목들을 추가해야 합니다.

 

다음과 같이 입력하면 secure.example.com에 대한 접근만 허용됩니다.

 

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="false">
        <domain includeSubdomains="true">secure.example.com</domain>
    </domain-config>
</network-security-config>

 

리스트를 모두 작성했다면 AndroidManifest에 android:networkSecurityConfig 속성으로 예외 리스트 파일을 설정합니다.

 

<application
    android:label="@string/app_name"
    ...
    android:networkSecurityConfig="@xml/network_security_config"

 

참고

Comments