无法解析符号:'HttpClient',即使在添加依赖项之后
Cannot resolve symbol: 'HttpClient', even after adding dependency
我尝试添加Apache commons libs
的各种依赖项。
我试过httpClient
、httpCore
和许多不同的版本。也来自外部 jar 以及来自 maven 的建议。
我在 gradle
中得到了这个:
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.3.5'
它仍然给我 Cannot resolve HttpClient
和 Cannot resolve HttpPost
等等
它不建议我导入 类。
执行 HTTP 的代码 post:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://fdsgsfdgs);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("id", null));
nameValuePairs.add(new BasicNameValuePair("name", "TestFromAppSpell"));
nameValuePairs.add(new BasicNameValuePair("damage", "12345"));
nameValuePairs.add(new BasicNameValuePair("heal", "50"));
nameValuePairs.add(new BasicNameValuePair("description", "Testtestest"));
nameValuePairs.add(new BasicNameValuePair("coordinates", coordinates));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
System.out.println("Http post done!");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Gradle 文件:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
dependencies {
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:gridlayout-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.3.5'
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 11
targetSdkVersion 23
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
我只需要在 build.gradle
文件的末尾添加它。
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
}
SDK 23 不再支持 HttpClient
。您必须使用 URLConnection
或降级到 SDK 22(编译 'com.android.support:appcompat-v7:22.2.0'
)
如果您需要 SDK 23,请将其添加到您的 gradle:
android {
useLibrary 'org.apache.http.legacy'
}
添加最后一个库,应该可以。它对我有用
我尝试添加Apache commons libs
的各种依赖项。
我试过httpClient
、httpCore
和许多不同的版本。也来自外部 jar 以及来自 maven 的建议。
我在 gradle
中得到了这个:
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.3.5'
它仍然给我 Cannot resolve HttpClient
和 Cannot resolve HttpPost
等等
它不建议我导入 类。
执行 HTTP 的代码 post:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://fdsgsfdgs);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("id", null));
nameValuePairs.add(new BasicNameValuePair("name", "TestFromAppSpell"));
nameValuePairs.add(new BasicNameValuePair("damage", "12345"));
nameValuePairs.add(new BasicNameValuePair("heal", "50"));
nameValuePairs.add(new BasicNameValuePair("description", "Testtestest"));
nameValuePairs.add(new BasicNameValuePair("coordinates", coordinates));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
System.out.println("Http post done!");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Gradle 文件:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
dependencies {
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:gridlayout-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.3.5'
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 11
targetSdkVersion 23
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
我只需要在 build.gradle
文件的末尾添加它。
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
}
HttpClient
。您必须使用 URLConnection
或降级到 SDK 22(编译 'com.android.support:appcompat-v7:22.2.0'
)
如果您需要 SDK 23,请将其添加到您的 gradle:
android {
useLibrary 'org.apache.http.legacy'
}
添加最后一个库,应该可以。它对我有用