Eld*_*dar 1 android kotlin okhttp
我正在尝试使用 okhttp 从 api 获取 json,当我尝试这样做时,应用程序崩溃了,并且出现运行时异常
\n\nFailed to parse XML configuration from network_security_config
我应该怎么做才能解决这个问题?\ni 在我的清单上拥有互联网权限。\ni\'v 试图在其他问题上搜索类似的内容,但找不到答案。
\n\n我尝试按照本教程进行操作:https://www.youtube.com/watch ?v=53BsyxwSBJk
\n\n但 XML 似乎有一些问题,但我不明白。
\n\n这是我的班级:
\n\nclass TestActivity : AppCompatActivity() {\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_test)\n\n val adapter = ArrayAdapter<User>(this, android.R.layout.simple_list_item_1)\n\n listViewTest.adapter = adapter\n val url = URL(myapi)\n\n val request = Request.Builder().url(url).build()\n val client = OkHttpClient()\n client.newCall(request).enqueue(object: Callback {\n\n override fun onResponse(call: Call, response: Response) {\n\n val body = response.body.toString()\n\n println(body)\n\n\n }\n\n\n override fun onFailure(call: Call, e: IOException) {\n\n\n }\n\n\n\n } )\n\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我的错误日志:
\n\n 2019-07-04 15:11:30.822 3994-3994/com.eldareini.kotlin.meet4match E/AndroidRuntime: FATAL EXCEPTION: main\nProcess: com.eldareini.kotlin.meet4match, PID: 3994\njava.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\n at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)\n at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)\n at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)\n at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)\n at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)\n at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)\n at android.os.Handler.dispatchMessage(Handler.java:106)\n at android.os.Looper.loop(Looper.java:193)\n at android.app.ActivityThread.main(ActivityThread.java:6669)\n at java.lang.reflect.Method.invoke(Native Method)\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)\n Caused by: java.lang.RuntimeException: Failed to parse XML configuration from network_security_config\n at android.security.net.config.XmlConfigSource.ensureInitialized(XmlConfigSource.java:89)\n at android.security.net.config.XmlConfigSource.getPerDomainConfigs(XmlConfigSource.java:55)\n at android.security.net.config.ManifestConfigSource.getPerDomainConfigs(ManifestConfigSource.java:45)\n at android.security.net.config.ApplicationConfig.ensureInitialized(ApplicationConfig.java:175)\n at android.security.net.config.ApplicationConfig.getTrustManager(ApplicationConfig.java:120)\n at android.security.net.config.RootTrustManagerFactorySpi.engineGetTrustManagers(RootTrustManagerFactorySpi.java:65)\n at javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:301)\n at okhttp3.internal.platform.Platform.platformTrustManager(Platform.kt:85)\n at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:211)\n at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:204)\n at com.eldareini.kotlin.meet4match.TestActivity.onCreate(TestActivity.kt:41)\n at android.app.Activity.performCreate(Activity.java:7136)\n at android.app.Activity.performCreate(Activity.java:7127)\n at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)\n at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)\n at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)\xc2\xa0\n at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)\xc2\xa0\n at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)\xc2\xa0\n at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)\xc2\xa0\n at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)\xc2\xa0\n at android.os.Handler.dispatchMessage(Handler.java:106)\xc2\xa0\n at android.os.Looper.loop(Looper.java:193)\xc2\xa0\n at android.app.ActivityThread.main(ActivityThread.java:6669)\xc2\xa0\n at java.lang.reflect.Method.invoke(Native Method)\xc2\xa0\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)\xc2\xa0\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)\xc2\xa0\n Caused by: android.security.net.config.XmlConfigSource$ParserException: Unknown certificates src. Should be one of system|user|@resourceVal at: Binary XML file line #6\n at android.security.net.config.XmlConfigSource.parseCertificatesEntry(XmlConfigSource.java:193)\n at android.security.net.config.XmlConfigSource.parseTrustAnchors(XmlConfigSource.java:208)\n at android.security.net.config.XmlConfigSource.parseConfigEntry(XmlConfigSource.java:262)\n at android.security.net.config.XmlConfigSource.parseNetworkSecurityConfig(XmlConfigSource.java:325)\n at android.security.net.config.XmlConfigSource.ensureInitialized(XmlConfigSource.java:83)\n at android.security.net.config.XmlConfigSource.getPerDomainConfigs(XmlConfigSource.java:55)\xc2\xa0\n at android.security.net.config.ManifestConfigSource.getPerDomainConfigs(ManifestConfigSource.java:45)\xc2\xa0\n at android.security.net.config.ApplicationConfig.ensureInitialized(ApplicationConfig.java:175)\xc2\xa0\n at android.security.net.config.ApplicationConfig.getTrustManager(ApplicationConfig.java:120)\xc2\xa0\n at android.security.net.config.RootTrustManagerFactorySpi.engineGetTrustManagers(RootTrustManagerFactorySpi.java:65)\xc2\xa0\n at javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:301)\xc2\xa0\n at okhttp3.internal.platform.Platform.platformTrustManager(Platform.kt:85)\xc2\xa0\n at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:211)\xc2\xa0\n at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:204)\xc2\xa0\n at com.eldareini.kotlin.meet4match.TestActivity.onCreate(TestActivity.kt:41)\xc2\xa0\n at android.app.Activity.performCreate(Activity.java:7136)\xc2\xa0\n at android.app.Activity.performCreate(Activity.java:7127)\xc2\xa0\n at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)\xc2\xa0\n at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)\xc2\xa0\n at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)\xc2\xa0\n at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)\xc2\xa0\n at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)\xc2\xa0\n at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)\xc2\xa0\n at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)\xc2\xa0\n at android.os.Handler.dispatchMessage(Handler.java:106)\xc2\xa0\n at android.os.Looper.loop(Looper.java:193)\xc2\xa0\n at android.app.ActivityThread.main(ActivityThread.java:6669)\xc2\xa0\n at java.lang.reflect.Method.invoke(Native Method)\xc2\xa0\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)\xc2\xa0\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
在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>
Run Code Online (Sandbox Code Playgroud)
将其添加到清单文件中的应用程序标记中。
android:networkSecurityConfig="@xml/config"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8970 次 |
| 最近记录: |