解决方法:无法从network_security_config解析XML配置

时间:2019-07-04 12:43:09

标签: android kotlin okhttp

我正在尝试使用okhttp从api获取json,当我尝试这样做时,应用程序崩溃了,并且我遇到了运行时异常

Failed to parse XML configuration from network_security_config

我应该怎么做才能解决?  我的清单上有INTERNET权限。 我试图在其他问题上寻找类似的东西,但找不到答案。

我尝试遵循本教程:https://www.youtube.com/watch?v=53BsyxwSBJk

但是XML似乎有一些问题,但我不理解。

这是我的课:

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) {


        }



    } )


  }
}

我的错误日志:

   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 个答案:

答案 0 :(得分:1)

在resources / xml下创建一个新文件,将其命名为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>

然后将其添加到application标签中的清单-

    android:networkSecurityConfig="@xml/network_security_config"

答案 1 :(得分:0)

在res中创建一个名为xml的文件夹,并创建一个名为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>

将此添加到清单文件的“应用程序”标记中。

 android:networkSecurityConfig="@xml/config"