Reputation: 727
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
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
I've added this to manifest.xml:-
<uses-permission android:name="android.permission.INTERNET"/>
I also added this to the manifest.xml:-
android:networkSecurityConfig="@xml/network_security_config"
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>
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:-
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.
Upvotes: 25
Views: 131660
Reputation: 119
Open AndroidManifest.xml
file at app/src/main folder, add this line of code inside application tag
android:networkSecurityConfig="@xml/network_security_config"
And now, you have to create a file named network_security_config.xml at this location app/src/main/res/xml, add following code into this file.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
If you have already do this, then just make sure that you have above code in your file. If your file contains domain named remove that line
Remove <domain includeSubdomains="true">secure.etc.com</domain> from the code.
And I have also done one more thing, I have removed this line of code from my AndroidManifest.xml file
android:usesCleartextTraffic="true"
which will be in your activity tag.
Upvotes: 2
Reputation: 31
i was also facing the same problem ,and tried many solutions(adding config etc things),but they didn't work for me..I sit and checked my code carefully, There was something wrong in the code which i've wrote to access the node. Not a syntactic error, it's node name missmatch.
Upvotes: 2
Reputation: 11477
Try these solutions
Solution 1)
Add the following attribute to the <application
tag in AndroidManifest.xml
:
android:usesCleartextTraffic="true"
Solution 2)
Add android:networkSecurityConfig="@xml/network_security_config"
to the <application
tag in 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.xml
in app/src/main/res/xml/
:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Refer this answer for more info: Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)
Upvotes: 81
Reputation: 1112
Edited Answer
Remove <domain includeSubdomains="true">secure.example.com</domain>
from the code.
Use just:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Try to set 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>
Upvotes: 21