0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Athira
  • 1,177
  • 3
  • 13
  • 35
  • `api it shows connection error.` What error? – AskNilesh Nov 19 '18 at 04:48
  • please show error message – TAHA SULTAN TEMURI Nov 19 '18 at 04:50
  • I am using AndroidNetworking for api calling. it only shows "connection error" – Athira Nov 19 '18 at 04:53
  • There might be different connection error. Like `Internet connection` ,`Server Connection` error. What kind of error are you getting? – Piyush Nov 19 '18 at 05:11
  • com.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to 115.124.98.77 not permitted – Athira Nov 19 '18 at 05:43
  • 3
    _Starting with Android 9.0 (API level 28), cleartext support is disabled by default_. So you need to add `android:usesCleartextTraffic="true"` in manifest file with in `application` flag. By default it will be `false`. – Piyush Nov 19 '18 at 05:46

2 Answers2

7

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">
        //your android components...
</application>
Athira
  • 1,177
  • 3
  • 13
  • 35
1

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 the XML folder in the res folder. and create a network_security_config.xml file in the 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>
Rahul
  • 3,293
  • 2
  • 31
  • 43
Pratibha Sarode
  • 1,819
  • 17
  • 17