org.apache.http.legacy doesn't work on API 28

16,954

Solution 1

To run org.apache.http.legacy perfectely in Android 9.0 Pie create an xml file res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
      <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
        <certificates src="system" />
       </trust-anchors>
      </base-config>
    </network-security-config>

And add 2 tags tag in your AndroidManifest.xml

android:networkSecurityConfig="@xml/network_security_config" android:name="org.apache.http.legacy"

<?xml version="1.0" encoding="utf-8"?>
 <manifest......>
  <application android:networkSecurityConfig="@xml/network_security_config">
   <activity..../> 
   ......
   ......
 <uses-library
        android:name="org.apache.http.legacy"
        android:required="false"/>
</application>

Also add useLibrary 'org.apache.http.legacy' in your app build gradle

android {
compileSdkVersion 28
defaultConfig {
    applicationId "your application id"
    minSdkVersion 15
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    useLibrary 'org.apache.http.legacy'
}

Solution 2

Step 1:
create an xml file in res/xml/network_security_config.xml and copy the below code in it,

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your.website.domain</domain> <!-- the domain you are using for api network call -->
    </domain-config>
</network-security-config>

Step 2:
now in your Manifest.xml add android:networkSecurityConfig="@xml/network_security_config" under the <application> tag. Done!

Solution 3

You just need to do two things:

1-Put in your AndroidManifest.xml the next line under <application>

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

2-Change the Url being invoke to HTTPS instead of HTTP

After I did that the error goes away and my app worked again.

Solution 4

I had the same problem. I resolved it by using HTTPS, instead of HTTP. Apparently, a secure connection is now required.

Solution 5

Apache HTTP client deprecation

With Android 6.0, we removed support for the Apache HTTP client. Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default.

Share:
16,954
Admin
Author by

Admin

Updated on July 25, 2022

Comments

  • Admin
    Admin almost 2 years

    To begin with, I was using org.apache.http functions for a while now and error came when I tried to launch my app on API 28. It's working on API 26 and API 23, but suddenly something is wrong with API 28. Did Google made some changes?

  • paakjis
    paakjis about 5 years
    This worked! I was using implementation "com.squareup.okhttp3:okhttp:3.11.0" implementation "com.squareup.okhttp3:okhttp-urlconnection:3.10.0" And on Android 9 requests always returned null.
  • AnteGemini
    AnteGemini almost 5 years
    The only thing that solved the issue for me. Spent almost a day making out with this problem. Thank you!
  • Hoang Viet
    Hoang Viet over 4 years
    Amazing, it works like a champ. You saved my day bro!
  • Fran Palomares
    Fran Palomares over 4 years
    Thanks, you saved my day!
  • Androider
    Androider over 4 years
    Thank you very much. Great Help, Bro.
  • Moeed Ahmed
    Moeed Ahmed about 4 years
    These 2 steps worked for me even in the mid of 2020.. ;-) Thanks man you saved my day