如何修复“不允许x的明文HTTP流量”

时间:2019-09-20 05:52:17

标签: java android-studio httpurlconnection

我正在尝试向http服务器发出发布请求,但是当我尝试获取输入流时,出现错误java.io.IOException: Cleartext HTTP traffic to x not permitted

我已经尝试将android:usesCleartextTraffic="true"放入清单中,并进行网络安全配置并将android:targetSandboxVersion设置为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输出

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)

任何帮助将不胜感激

8 个答案:

答案 0 :(得分:1)

尝试仅使用

android:usesCleartextTraffic="true"

删除此2

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

mine仅在使用android:usesCleartextTraffic =“ true”

的任何API中均可工作

答案 1 :(得分:0)

可能是您犯了一个错误。这是从其他来源获得的正确格式。

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

如果您具有android:targetSandboxVersion,则将其减小为1

AndroidManifest.xml-

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

答案 2 :(得分:0)

在您的项目中创建文件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>

如果您有android:targetSandboxVersion,也可以将其减小为1

答案 3 :(得分:0)

解决了我自己的问题,使我为后代感到羞耻。

无意间在<domain includeSubdomains="true"></domain>中放置了一个网址,而不是一个域。

答案 4 :(得分:0)

我遇到了同样的问题,我在And中将android版本降低到25,从而解决了该问题,该应用程序在android 9及更低版本的设备上运行良好。

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

答案 5 :(得分:0)

不喜欢这种方式可能会导致运行时错误 做最简单的方法; 转到android native项目,您可以在此处看到Properties,然后单击AssemblyInfo.cs文件,然后在下面编辑此部分

[assembly:Application(UsesCleartextTraffic = true)]

我的代码下方的示例:-

 using System.Reflection;
    using System.Runtime.CompilerServices;

    using System.Runtime.InteropServices;

    using Android.App;

    // General Information about an assembly is controlled through the following 
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("ZATWEBO.Android")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("ZATWEBO.Android")]
    [assembly: AssemblyCopyright("Copyright ©  2014")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    [assembly: ComVisible(false)]

    // Version information for an assembly consists of the following four values:
    //
    //      Major Version
    //      Minor Version 
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Build and Revision Numbers 
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]

    // Add some common permissions, these can be removed if not needed
    [assembly: UsesPermission(Android.Manifest.Permission.Internet)]
    [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
    [assembly: Application(UsesCleartextTraffic = true)]

注意:此更改仅需要android 9 Pie或更高版本

答案 6 :(得分:0)

我在Android Manifest文件中添加了以下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config>
            <trust-anchors>
                <certificates src="system">
                </certificates>
            </trust-anchors>
        </base-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">http://YourURL/api/</domain>
        </domain-config>
    </network-security-config>

答案 7 :(得分:-1)

请确保您对发帖和原始标头都具有https请求。如果您有一个http请求,而另一个是htps://,那么它将无法正常工作,而我遇到的问题是相同的错误。