23

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.

Quick learner
  • 10,632
  • 4
  • 45
  • 55
Markus
  • 665
  • 2
  • 9
  • 22
  • Unless you want to restrict the urls your app can hit for some reason, you don't really need to make a custom network security config- the default is almost always sufficient. The only time it isn't is if you have an untrusted cert for your service, which is basically never these days with Let's Encrypt being almost free. – Gabe Sechan Dec 31 '18 at 07:30
  • Add this in application tag:- android:usesCleartextTraffic="true" – Chirag Nahar Dec 31 '18 at 07:35
  • Thanks, I did start without it, I just added the network security in an effort to try and get it to work. – Markus Dec 31 '18 at 07:37
  • Please share the device os , modal and also api usage ie code where you are calling the api method , Thanks – Quick learner Dec 31 '18 at 07:48
  • Changed to usesCleartextTraffic="true", still gives same code 400 error. – Markus Dec 31 '18 at 07:48
  • Do study on this - https://developer.android.com/training/articles/security-config – Al-Amin Dec 31 '18 at 07:58

3 Answers3

70

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>

enter image description here

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

jorisw
  • 875
  • 1
  • 11
  • 17
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • 1
    Sorry still no luck. I'm wondering if it may be an issue with how I'm making the request to the server. It is a simple GET request, so I'm not sure why that could be. I'll look into it further. – Markus Dec 31 '18 at 08:00
  • Can you explain in more details what are each of these solutions mean? – Shachar Har-Shuv Oct 21 '19 at 10:17
  • Man, for someone who specializes in iOS development, what is up with the need for that long path location? You saved me with that detail which was missing from the Android guide. Thanks! – David May 14 '20 at 20:26
16

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>

And It will work for any URL or IP.

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>
android developer
  • 114,585
  • 152
  • 739
  • 1,270
Insane Developer
  • 1,014
  • 10
  • 19
  • 1
    Thanks for the reply. I'm still getting:- Using Network Security Config from resource network_security_config debugBuild: true, Error response code: 400 – Markus Dec 31 '18 at 07:32
  • @Markus how did you solve it? I am also getting the same issue in one of my projects. There is something related to the server. As I have solved this using same solution for a different project. Do let me know. – Insane Developer Jan 02 '19 at 12:18
  • The issues ended up being an error in my URL string requesting the information, which caused the Error 400. Once I corrected my URL API request string it worked fine. – Markus Jan 03 '19 at 23:32
  • But, My Url was correct, and I banged my head but cannot resolve. So, I tried the code I am adding as an answer. It works for any url or IP. – Insane Developer Jan 04 '19 at 05:45
  • I meant the error was in the URL request, the site could not understand the request, so it returned an error 400. – Markus Jan 04 '19 at 21:10
  • About the URL , it was set to `cleartextTrafficPermitted="false"` . I've edited the answer. Now it works fine. – android developer May 16 '19 at 07:01
1

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.