API call in Android 9 configuration devices and emulator shows connection error

▼魔方 西西 提交于 2019-12-02 21:44:22

问题


I am using Android 9 emulator, it has internet connection from system and I have added internet permission in manifest. When I try to call api it shows error message as : connection error.


回答1:


As piyush commented in my question the answer is to add android:usesCleartextTraffic="true" in manifest file with in application tag

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.mypackage">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/appicon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
 <activity />
</application>



回答2:


Just add below in manifest's application tag

android:networkSecurityConfig="@xml/network_security_config"

Add below code inside the application where we add activities

 <uses-library android:name="org.apache.http.legacy" android:required="false"/>

Add xml folder in res folder. and create a network_security_config.xml file in xml folder.

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


来源:https://stackoverflow.com/questions/53368470/api-call-in-android-9-configuration-devices-and-emulator-shows-connection-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!