Java NetworkSecurityConfig:未指定网络安全配置,使用平台默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53984725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:57:44  来源:igfitidea点击:

NetworkSecurityConfig: No Network Security Config specified, using platform default

javaandroidasp.net-web-apinetworking

提问by Markus

I'm trying to connect to either of these places and get the JSON data:-

我正在尝试连接到这些地方中的任何一个并获取 JSON 数据:-

https://content.guardianapis.com/search?q=debate&tag=politics/politics&from-date=2014-01-01&api-key=test
https://content.guardianapis.com/search?q=debates&api-key=test
https://content.guardianapis.com/search?api-key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx

All three I can get to via the browser, but when I try to access them via an android app I get the following errors:-

我可以通过浏览器访问所有三个,但是当我尝试通过 android 应用访问它们时,我收到以下错误:-

NetworkSecurityConfig: No Network Security Config specified, using platform default
Error response code: 400

I've added this to manifest.xml:-

我已将此添加到 manifest.xml:-

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

I also added this to the manifest.xml:-

我还将它添加到 manifest.xml:-

android:networkSecurityConfig="@xml/network_security_config"

And created res/xml/network_security_config.xml, which contains:-

并创建了 res/xml/network_security_config.xml,其中包含:-

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">content.guardianapis.com</domain>>
    </domain-config>

Which changes the error to:-

这将错误更改为:-

D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
Error response code: 400

I know it's missing:-

我知道它不见了:-

<trust-anchors>
    <certificates src="@raw/my_ca"/>
</trust-anchors>

but I have no idea where or what the certificate would be or if it's needed.

但我不知道证书在哪里或什么,或者是否需要。

Not really sure what is going on, any help would be appreciated.

不太确定发生了什么,任何帮助将不胜感激。

IN ADDITION:- I am able to connect fine and get the JSON from this URL:-

此外:- 我能够正常连接并从此 URL 获取 JSON:-

https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10

All I get is this:-

我得到的只是这个:-

No Network Security Config specified, using platform default

Buy not error 400 and gets through with a 200 instead. So it makes me think there is something weird going on, but not sure where.

不要购买错误 400,而是通过 200。所以这让我觉得发生了一些奇怪的事情,但不确定在哪里。

采纳答案by Quick learner

Try these solutions

试试这些解决方案

Solution 1)

解决方案1)

Add the following attribute to the <applicationtag in AndroidManifest.xml:

将以下属性添加到 中的<application标记AndroidManifest.xml

android:usesCleartextTraffic="true"

Solution 2)

解决方案2

Add android:networkSecurityConfig="@xml/network_security_config"to the <applicationtag in app/src/main/AndroidManifest.xml:

添加android:networkSecurityConfig="@xml/network_security_config"<application标签中app/src/main/AndroidManifest.xml

<application
        android:name=".ApplicationClass"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

With a corresponding network_security_config.xmlin app/src/main/res/xml/:

具有相应的network_security_config.xmlin app/src/main/res/xml/

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

enter image description here

在此处输入图片说明

Refer this answer for more info: Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)

有关更多信息,请参阅此答案: 下载管理器无法在 Android Pie 9.0 (Xiaomi mi A2) 中运行

回答by Insane Developer

Edited AnswerRemove <domain includeSubdomains="true">secure.example.com</domain>from the code.

编辑后的答案<domain includeSubdomains="true">secure.example.com</domain>从代码中 删除 。

Use just:

仅使用:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

And It will work for any URL or IP.

它适用于任何 URL 或 IP。

Try to set cleartextTrafficPermitted=false

尝试设置 cleartextTrafficPermitted=false

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