我有一个简单的类写在Android Studio:

package com.mysite.myapp;

import org.apache.http.client.HttpClient;

public class Whatever {
    public void headBangingAgainstTheWallExample () {
        HttpClient client = new DefaultHttpClient();
    }
}

从这里我得到了以下编译时错误:

无法解析HttpClient符号

HttpClient不是包含在Android Studio SDK中吗?即使不是,我也像这样把它添加到我的Gradle构建中:

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

不管有没有最后一个编译行,错误都是一样的。我错过了什么?


当前回答

在SDK级别23中使用Apache HTTP:

顶层构建。Gradle - /build.gradle

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0' 
        // Lowest version for useLibrary is 1.3.0
        // Android Studio will notify you about the latest stable version
        // See all versions: http://jcenter.bintray.com/com/android/tools/build/gradle/
    }
    ...
}

Android工作室关于gradle更新的通知:

特定于模块的构建。Gradle - /app/build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}

其他回答

你只需要加一行

useLibrary 'org.apache.http.legacy'

为构建。例如gradle(Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.0"

    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.avenues.lib.testotpappnew"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}

适用于android API 28及更高版本 在Manifest.xml应用程序标签内

    <application
    .
    .
    .

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

试试这个 为我工作 将此依赖项添加到构建中。gradle文件

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

在sdk 23中不再支持HttpClient。Android 6.0 (API Level 23)版本删除了对Apache HTTP客户端的支持。 你必须使用

android {
    useLibrary 'org.apache.http.legacy'
    .
    .
    .

并在您的依赖项中添加以下代码片段:

//http web服务的最终解决方案(包括文件上传)

compile('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
}
 compile 'org.apache.httpcomponents:httpclient-android:4.3.5'

它也将帮助你,当你使用使用多亲子文件上传。

ApacheHttp Client在v23 sdk中被移除。您可以使用HttpURLConnection或第三方Http客户端,如OkHttp。

参考:https://developer.android.com/preview/behavior-changes.html#behavior-apache-http-client