NetworkSecurityConfig:未使用平台默认值指定网络安全配置,错误响应代码:400 [英] NetworkSecurityConfig: No Network Security Config specified, using platform default Error response code: 400
问题描述
我正在尝试连接到这些地方之一并获取JSON数据:-
I'm trying to connect to either of these places and get the JSON data:-
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
所有这三个我都可以通过浏览器访问,但是当我尝试通过Android应用访问它们时,出现以下错误:-
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:-
NetworkSecurityConfig: No Network Security Config specified, using platform default
Error response code: 400
我已将其添加到manifest.xml:-
I've added this to manifest.xml:-
<uses-permission android:name="android.permission.INTERNET"/>
我还将其添加到manifest.xml:-
I also added this to the manifest.xml:-
android:networkSecurityConfig="@xml/network_security_config"
并创建了res/xml/network_security_config.xml,其中包含:-
And created res/xml/network_security_config.xml, which contains:-
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">content.guardianapis.com</domain>>
</domain-config>
将错误更改为:-
D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
Error response code: 400
我知道它不见了:-
<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.
另外:- 我可以很好地连接并从以下URL获取JSON:-
IN ADDITION:- I am able to connect fine and get the JSON from this URL:-
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
不购买错误400,而是购买200.因此,这让我觉得有些奇怪的事情正在发生,但不确定在哪里.
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.
推荐答案
尝试这些解决方案
解决方案1 )
在AndroidManifest.xml
中的<application
标记中添加以下属性:
Add the following attribute to the <application
tag in AndroidManifest.xml
:
android:usesCleartextTraffic="true"
解决方案2 )
在app/src/main/AndroidManifest.xml
中的<application
标记中添加android:networkSecurityConfig="@xml/network_security_config"
:
<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">
在app/src/main/res/xml/
中带有相应的network_security_config.xml
:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
有关更多信息,请参考此答案: 下载管理器在Android Pie 9.0(Xiaomi mi A2)中无法使用
Refer this answer for more info: Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)
这篇关于NetworkSecurityConfig:未使用平台默认值指定网络安全配置,错误响应代码:400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!