HttpClient不会在Android Studio中导入

And*_*Dev 345 android gradle apache-httpclient-4.x android-gradle-plugin

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

package com.mysite.myapp;

import org.apache.http.client.HttpClient;

public class Whatever {
    public void headBangingAgainstTheWallExample () {
        HttpClient client = new DefaultHttpClient();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Cannot resolve symbol 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'
}
Run Code Online (Sandbox Code Playgroud)

无论有没有最后一个编译行,错误都是一样的.我错过了什么?

Ily*_*okh 792

HttpClient在sdk 23中不再支持.你必须使用URLConnection或降级到sdk 22(compile 'com.android.support:appcompat-v7:22.2.0')

如果您需要sdk 23,请将其添加到您的gradle中:

android {
    useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)

你也可以尝试下载并包括HttpClient的罐子直接到你的项目,或者使用OkHttp代替

  • 在我的情况下它没有做任何事情.怎么会? (20认同)
  • 公告链接:https://developer.android.com/preview/behavior-changes.html#behavior-apache-http-client (3认同)
  • 添加它时无法解析useLibrary.我的目标是23,我错过了什么? (2认同)

str*_*aya 155

HttpClient在API级别22中已弃用,在API级别23中已删除.如果必须,您仍可在API级别23及更高版本中使用它,但最好转移到支持的方法来处理HTTP.因此,如果您使用23进行编译,请在build.gradle中添加:

android {
    useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)

  • `android {useLibrary'org.apache.http.legacy'}`**[当你添加这个]****[1]**'org.apache.http'android-studio仍有提示:'无法解析符号'**[2]**这使得android-studio(与sdk23)可以构建.虽然有提示(无法解析符号)**[3]**如果你添加jar,它没有提示,可以构建,但!!!无法运行,因为它有两个副本'org.apache.http' (4认同)

Vin*_*nay 57

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'
    ...
}
Run Code Online (Sandbox Code Playgroud)


And*_*ICE 47

要使用Apache HTTP for SDK Level 23:

顶级build.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/
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

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

特定于模块的build.gradle - /app/build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}
Run Code Online (Sandbox Code Playgroud)


Pri*_*shi 30

试试这个为我工作将此依赖项添加到build.gradle文件中

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
Run Code Online (Sandbox Code Playgroud)


ful*_*oon 16

1-下载Apache jar文件(截至此答案)4.5.zip文件来自:https://hc.apache.org/downloads.cgi?Preferred = http%3A%2F%2Fapache.arvixe.com%2F

2-打开zip文件将jar文件复制到libs文件夹中.你可以找到它,如果你到项目的顶部说"Android",当你点击它时你会找到一个列表.所以,

Android - >项目 - > app - > libs

,然后把罐子放在那里.

3-在build.gradle(Module:app)中添加

compile fileTree(dir: 'libs', include: ['*.jar'])
Run Code Online (Sandbox Code Playgroud)

 dependency { 
   }
Run Code Online (Sandbox Code Playgroud)

4-在java类中添加以下导入:

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.CoreProtocolPNames;
Run Code Online (Sandbox Code Playgroud)


小智 14

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

android {
    useLibrary 'org.apache.http.legacy'
    .
    .
    .
Run Code Online (Sandbox Code Playgroud)

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

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

compile('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
}
 compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
Run Code Online (Sandbox Code Playgroud)

当您使用"使用MultipartEntity进行文件上载"时,它也会对您有所帮助.


Cat*_*lin 7

在API 22中它们被弃用了,在API 23中它们完全删除了它们,如果你不需要新添加的所有花哨的东西,只需使用apache之前集成的API中的.jar文件,这是一个简单的解决方法,但是作为分隔的.jar文件:

1. http://hc.apache.org/downloads.cgi
2. download httpclient 4.5.1, the zile file
3. unzip all files
4. drag in your project httpclient-4.5.1.jar, httpcore-4.4.3.jar and httpmime-4.5.1.jar
5. project, right click, open module settings, app, dependencies, +, File dependency and add the 3 files
6. now everything should compile properly
Run Code Online (Sandbox Code Playgroud)


小智 7

自 2021 年 4 月起,您可以使用以下内容:

在您的应用程序 gradle 中,在“依赖项 {”下添加以下内容:

implementation 'org.apache.httpcomponents:httpcore:4.4.10'
implementation 'org.apache.httpcomponents:httpclient:4.5.6'
Run Code Online (Sandbox Code Playgroud)

在您的 Java 活动中,添加以下导入:

import org.apache.http.client.HttpClient;
Run Code Online (Sandbox Code Playgroud)

然后您应该能够将 HttpClient 添加到您的方法中。


Mos*_*eef 6

您只需将其添加到Gradle依赖项:

compile "org.apache.httpcomponents:httpcore:4.3.2"
Run Code Online (Sandbox Code Playgroud)

  • 这有帮助.但是如果你想使用像`HttpGet`这样的其他类,还是不够.为此,我使用`compile'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'` (2认同)

小智 6

Android 6.0(API Level 23)版本删除了对Apache HTTP客户端的支持.因此,您不能直接在API 23中使用此库.但有一种方法可以使用它.在build.gradle文件中添加useLibrary'org.apache.http.legacy',如下所示 -

android {
    useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,您可以应用以下hack-

- 将/ SDK/android-23/Android SDK目录的可选路径中的org.apache.http.legacy.jar复制到项目的app/libs文件夹中.

- 现在在build.gradle文件的dependencies {}部分中添加编译文件('libs/org.apache.http.legacy.jar').


A.B*_*ami 6

如果要导入某些类,例如:

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;
Run Code Online (Sandbox Code Playgroud)

您可以在build.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'

}
Run Code Online (Sandbox Code Playgroud)


Kir*_*tan 5

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

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


Nil*_*hal 5

你只需要添加一行

useLibrary 'org.apache.http.legacy'
Run Code Online (Sandbox Code Playgroud)

进入build.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'
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

535678 次

最近记录:

6 年,9 月 前