1

I'm trying to get json from an api, using okhttp and when i try to do so the app is crushing and i get a runtime Exception

Failed to parse XML configuration from network_security_config

what should i do to solve it? i have INTERNET permission on my manifest. i'v tried to search for something similar on other questions and could't find an answer.

I've tried to follow this tutorial: https://www.youtube.com/watch?v=53BsyxwSBJk

but it seems that there is some problem with the XML but i don't understand it.

here is my class:

class TestActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_test)

    val adapter = ArrayAdapter<User>(this, android.R.layout.simple_list_item_1)

    listViewTest.adapter = adapter
    val url = URL(myapi)

    val request = Request.Builder().url(url).build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback {

        override fun onResponse(call: Call, response: Response) {

            val body = response.body.toString()

            println(body)


        }


        override fun onFailure(call: Call, e: IOException) {


        }



    } )


  }
}

my error log:

   2019-07-04 15:11:30.822 3994-3994/com.eldareini.kotlin.meet4match E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.eldareini.kotlin.meet4match, PID: 3994
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eldareini.kotlin.meet4match/com.eldareini.kotlin.meet4match.TestActivity}: java.lang.RuntimeException: Failed to parse XML configuration from network_security_config
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.RuntimeException: Failed to parse XML configuration from network_security_config
    at android.security.net.config.XmlConfigSource.ensureInitialized(XmlConfigSource.java:89)
    at android.security.net.config.XmlConfigSource.getPerDomainConfigs(XmlConfigSource.java:55)
    at android.security.net.config.ManifestConfigSource.getPerDomainConfigs(ManifestConfigSource.java:45)
    at android.security.net.config.ApplicationConfig.ensureInitialized(ApplicationConfig.java:175)
    at android.security.net.config.ApplicationConfig.getTrustManager(ApplicationConfig.java:120)
    at android.security.net.config.RootTrustManagerFactorySpi.engineGetTrustManagers(RootTrustManagerFactorySpi.java:65)
    at javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:301)
    at okhttp3.internal.platform.Platform.platformTrustManager(Platform.kt:85)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:211)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:204)
    at com.eldareini.kotlin.meet4match.TestActivity.onCreate(TestActivity.kt:41)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
 Caused by: android.security.net.config.XmlConfigSource$ParserException: Unknown certificates src. Should be one of system|user|@resourceVal at: Binary XML file line #6
    at android.security.net.config.XmlConfigSource.parseCertificatesEntry(XmlConfigSource.java:193)
    at android.security.net.config.XmlConfigSource.parseTrustAnchors(XmlConfigSource.java:208)
    at android.security.net.config.XmlConfigSource.parseConfigEntry(XmlConfigSource.java:262)
    at android.security.net.config.XmlConfigSource.parseNetworkSecurityConfig(XmlConfigSource.java:325)
    at android.security.net.config.XmlConfigSource.ensureInitialized(XmlConfigSource.java:83)
    at android.security.net.config.XmlConfigSource.getPerDomainConfigs(XmlConfigSource.java:55) 
    at android.security.net.config.ManifestConfigSource.getPerDomainConfigs(ManifestConfigSource.java:45) 
    at android.security.net.config.ApplicationConfig.ensureInitialized(ApplicationConfig.java:175) 
    at android.security.net.config.ApplicationConfig.getTrustManager(ApplicationConfig.java:120) 
    at android.security.net.config.RootTrustManagerFactorySpi.engineGetTrustManagers(RootTrustManagerFactorySpi.java:65) 
    at javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:301) 
    at okhttp3.internal.platform.Platform.platformTrustManager(Platform.kt:85) 
    at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:211) 
    at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:204) 
    at com.eldareini.kotlin.meet4match.TestActivity.onCreate(TestActivity.kt:41) 
    at android.app.Activity.performCreate(Activity.java:7136) 
    at android.app.Activity.performCreate(Activity.java:7127) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

2 Answers 2

5

Create a folder named xml in res and create a file named config.xml

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

add this in Application tag in manifest file.

 android:networkSecurityConfig="@xml/config"
Sign up to request clarification or add additional context in comments.

Comments

1

Create a new file under resources/xml naming it as network_security_config.xml -

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

Then add this to the Manifest in the application tag -

    android:networkSecurityConfig="@xml/network_security_config"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.