How to fix 'Cleartext HTTP traffic to x not permitted'

…衆ロ難τιáo~ 提交于 2020-03-03 03:43:42

问题


I'm trying to make a post request to an http server, but when I try to get an input stream I get the error java.io.IOException: Cleartext HTTP traffic to x not permitted

I've already tried putting android:usesCleartextTraffic="true" in my manifest, as well as making a network security config and setting the android:targetSandboxVersion to 1

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">Server adress</domain>
    </domain-config>
</network-security-config>

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.packagename"
    android:targetSandboxVersion="1">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config"
        >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Logcat output

D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
W/System.err: java.io.IOException: Cleartext HTTP traffic to x not permitted
W/System.err:     at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:124) 
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)

Any help would be appreciated


回答1:


Try using just

android:usesCleartextTraffic="true"

delete this 2

android:targetSandboxVersion="1"
android:networkSecurityConfig="@xml/network_security_config"

mine is working in any API just using android:usesCleartextTraffic="true"




回答2:


Create a file in your project res/xml/security_config.xml -

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain>
    </domain-config>
</network-security-config>

AndroidManifest.xml -

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:networkSecurityConfig="@xml/security_config"
        ...>
        ...
    </application>
</manifest>

Also if you have android:targetSandboxVersion in then reduce it to 1




回答3:


May be you made a mistake. Here is a correct format got it from another source.

 <?xml version="1.0" encoding="utf-8"?>
 <network-security-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain>
</domain-config>

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>

If you have android:targetSandboxVersion in then reduce it to 1

AndroidManifest.xml -

   <?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET" />
...




回答4:


Solved my own problem, airing my shame for posterity.

Accidentally put a url in <domain includeSubdomains="true"></domain> instead of a domain.




回答5:


I had the same problem and I solved it by lowering the android version to 25 in And, the application works well on devices with android 9 and smaller.

<uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="25" />



回答6:


If you are working in your local machine make sure that you are requesting to 10.0.2.2 instead of localhost (this is very important)

Then make sure that your AndroidManfest.xml file has following lines:

<uses-permission android:name="android.permission.INTERNET" />

<application
        ... 
        android:networkSecurityConfig="@xml/network_security_config">

and your network_security_config.xml file must look like this:

<?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>

or if you need to allow request to specific domains you could modify network_security_config.xml file with:

<?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>

Both network_security_config.xml configurations works for me. I'm working on my local development enviroment.



来源:https://stackoverflow.com/questions/58022495/how-to-fix-cleartext-http-traffic-to-x-not-permitted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!