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

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


当前回答

如果你想导入一些类,比如:

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

您可以在构建中添加以下行。gradle (gradle依赖项)

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

    .
    .
    .

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

}

其他回答

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'
    ...
}

HttpClient在API Level 22中已弃用,并在API Level 23中被移除。如果你必须,你仍然可以在API级别23及以上使用它,但是最好转移到受支持的方法来处理HTTP。所以,如果你用23编译,在build.gradle中添加这个:

android {
    useLibrary("org.apache.http.legacy")
}

在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'

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

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

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

另一种方法是如果你有httpclient。jar文件你就可以 这样的:

将.jar文件粘贴到项目的“libs文件夹”中。然后在gradle中添加这一行到build.gradle(模块:app)

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile files('libs/httpcore-4.3.3.jar')
}