Can';在Android Studio中导入org.apache.http.HttpResponse
我想在Android Studio中使用这些库:Can';在Android Studio中导入org.apache.http.HttpResponse,android,eclipse,android-studio,Android,Eclipse,Android Studio,我想在Android Studio中使用这些库: 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; 我正在学习一个视频教程,教程中的导师正在使用Eclipse,所以我知道它是有效的 但是,
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;
我正在学习一个视频教程,教程中的导师正在使用Eclipse,所以我知道它是有效的
但是,为了使用它们,我还需要向Android Studio添加哪些额外的东西/库呢?HttpClient在Android 5.1中被弃用,并从Android 6.0的Android SDK中删除。虽然有,但你真的需要换一种方式。“其他东西”可以是:
- 内置的经典Java
HttpUrlConnection
- Apache的独立打包
- (我的建议)
必要时,您可以通过在模块的
build.gradle
文件中的android
闭包中使用useLibrary'org.apache.http.legacy'
来启用遗留API。然而,谷歌多年来一直建议人们停止使用Android内置的HttpClient,因此,这最多应该是一个权宜之计,而你需要更持久地转向另一个API。HttpClient在sdk 23中被弃用
您必须在URLConnection上移动或在sdk下移动到22
您仍然需要具有更新gradle sdk 23的HttpClient
您必须在app/gradle中将HttpClient的依赖项添加为
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'
...
}
主build.gradle-/build.gradle
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
// Versions: http://jcenter.bintray.com/com/android/tools/build/gradle/
}
...
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
useLibrary 'org.apache.http.legacy'
...
}
特定于模块的build.gradle-/app/build.gradle
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
// Versions: http://jcenter.bintray.com/com/android/tools/build/gradle/
}
...
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
useLibrary 'org.apache.http.legacy'
...
}
如果您要开始开发,请从square转到OkHttp,否则,如果您需要保持以前的代码运行,请将遗留库添加到您的项目依赖项中:
dependencies {
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}
根据Apache网站,如果您使用Android API 23或更高版本,这是您需要包含的Gradle依赖项:
dependencies {
compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
}
资料来源:
使用此选项:-
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
你的gradle文件中有依赖项吗?无论谁使用异步HTTP库和SDK 23,你都需要遵循@TejaDroid的答案。将其添加到app/gradleBut,但编译'com.android.support:appcompat-v7:23.0.1'@garima,我认为需要同样的
buildToolsVersion
和编译lib版本。意味着如果您有buildToolsVersion
=23.0.2
,则使用appcompat
version23.0.2
。我们不应修改“主构建梯度”。正确的??无论如何,我在app/build/gradle中插入了一行:useLibrary'org.apache.http.legacy',它对我来说很好。另一个答案已经包括了这个。这里有一个更新的@Miguelos:我编辑了答案以包含更新的链接——谢谢!