apache http imports not working [duplicate]

2019-09-10 13:38发布

问题:

This question already has an answer here:

  • org.apache.http.entity.FileEntity is deprecated in Android 6 (Marshmallow) 3 answers

I am using useLibrary 'org.apache.http.legacy' in my build.gradle but some imports are not working

Here are my imports:

import org.apache.http.HttpEntity; // Working
import org.apache.http.HttpResponse; // Working
import org.apache.http.client.ClientProtocolException; // Not Working
import org.apache.http.client.methods.HttpPost; // Not Working
import org.apache.http.impl.client.DefaultHttpClient; // Not Working
import org.apache.http.params.BasicHttpParams; // Working

Here is my build.gradle

apply plugin: 'com.android.application'

android {
  compileSdkVersion 23
  buildToolsVersion "23.0.2"

  useLibrary 'org.apache.http.legacy'

defaultConfig {
    applicationId "com.user.app"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
     }
   }
 } 

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.android.support:appcompat-v7:23.1.0'
  compile 'org.apache.httpcomponents:httpcore:4.4.3'
}

回答1:

Try to add org.apache.http.legacy.jar to your libs folder.

You can find this jar in Android/Sdk/platforms/android-23/optional

Add this line to your app.gradle file

compile fileTree(dir: 'libs', include: ['*.jar'])

And delete useLibrary 'org.apache.http.legacy'

This way Im using library without any problem