我正在尝试连接到这两个地方中的任何一个并获取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
所有这三个我都可以通过浏览器访问,但当我试图通过android应用程序访问它们时,我得到了以下错误:
NetworkSecurityConfig: No Network Security Config specified, using platform default
Error response code: 400
我已经将这个添加到了manifest.xml中:
<uses-permission android:name="android.permission.INTERNET"/>
我还将以下内容添加到了manifest.xml中:
android:networkSecurityConfig="@xml/network_security_config"
并创建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>
这会将错误更改为:-
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>
但我不知道证书会在哪里,或者证书是什么,或者是否需要它。
不太确定发生了什么,任何帮助都将不胜感激。
此外:-我可以很好地连接,并从这个URL获得JSON:-
https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10
我得到的只是这个:
No Network Security Config specified, using platform default
买的不是错误400,而是200。所以这让我觉得有些奇怪的事情发生了,但不确定在哪里。
发布于 2018-12-31 15:47:12
尝试这些解决方案
解决方案1)
将以下属性添加到AndroidManifest.xml
中的<application
标记
android:usesCleartextTraffic="true"
解决方案2)
将android:networkSecurityConfig="@xml/network_security_config"
添加到app/src/main/AndroidManifest.xml
中的<application
标记
<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>
有关更多信息,请参阅以下答案:Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)
发布于 2018-12-31 15:27:21
编辑的答案从代码中删除<domain includeSubdomains="true">secure.example.com</domain>
。
只使用:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
它将适用于任何URL或IP。
尝试设置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>
发布于 2021-07-20 20:30:50
我也面临着同样的问题,并尝试了许多解决方案(添加配置等),但它们对我不起作用。我坐下来仔细检查了我的代码,我写的访问节点的代码中有一些错误。这不是语法错误,而是节点名称不匹配。
https://stackoverflow.com/questions/53984725
复制相似问题