我有一个简单的类写在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'
}

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


当前回答

TejaDroid的答案在下面的链接中帮助了我。不能在Android Studio导入org.apache.http.HttpResponse

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'

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

其他回答

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

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

错误:(30,0)Gradle DSL方法未找到:'classpath()' 可能原因:项目“cid”可能使用了不包含该方法的Android Gradle插件版本(例如。'testCompile'在1.1.0中添加)。 将插件升级到2.3.3版本并同步项目“cid”项目可能使用了不包含该方法的Gradle版本。 打开Gradle包装文件构建文件可能缺少一个Gradle插件。 应用Gradle插件

你的项目中有哪个API目标?androidhttpclient仅适用于API级别8 <。 请看这里

享受你的代码:)

HttpClient在sdk 23和23+中不支持。

如果你需要使用到sdk 23,添加以下代码到你的gradle:

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

这对我很有用。希望对你有用。

TejaDroid的答案在下面的链接中帮助了我。不能在Android Studio导入org.apache.http.HttpResponse

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'

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