简体   繁体   English

成功Gradle同步后无法在Android Studio中使用HttpPost

[英]can't use HttpPost in Android Studio after successful Gradle sync

I'm trying to use HttpPost as I did in the past in Android. 我试图像过去在Android中一样使用HttpPost。 I realize that it has been removed from recent Android versions. 我意识到它已从最新的Android版本中删除。 Following the solutions on a similar StackOverflow question, I added this to my Gradle dependencies: 按照类似的StackOverflow问题的解决方案,我将其添加到了Gradle依赖项中:

compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'

After a Gradle sync that complained of no errors, I still cannot import the HttpPost program. 在抱怨没有错误的Gradle同步之后,我仍然无法导入HttpPost程序。 In fact, the org.apache.http.client.methods package does not resolve at all. 实际上,org.apache.http.client.methods包根本无法解析。

I also tried what the Apache HttpClient website suggested, which was to use this instead in my Gradle dependencies: 我还尝试了Apache HttpClient网站的建议,那就是在我的Gradle依赖项中使用它:

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'

Once again, the sync went fine, and this time the package resolved, but not the HttpPost program. 再次,同步进行得很好,这一次软件包解决了,但是HttpPost程序却没有。

在此处输入图片说明

I'd be grateful for any suggestions. 如有任何建议,我将不胜感激。

只需在android插件内的build.gradle添加以下行:

useLibrary 'org.apache.http.legacy'

The Apache HttpClient 4.3 for Android meant to be an update for the legacy 4.0 alpha version that was included in early Android versions. 适用于Android的Apache HttpClient 4.3意味着对早期Android版本中包含的旧版4.0 alpha版本的更新。 To avoid naming conflicts some on the updated classes got the postfix HC4 , like HttpPostHC4 for example, which one had to use instead. 为了避免命名冲突,一些更新的类上增加了后缀HC4 ,例如HttpPostHC4 ,而不得不使用它。

But now that the old HttpClient was removed form Android, the old classes like HttpPost do not exist anymore. 但是,既然旧的HttpClient已从Android中删除,那么HttpPost类的旧类就不再存在。

The official way to get the HttpClient library on newer Android version is to use: 在较新的Android版本上获取HttpClient库的官方方法是使用:

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

as documented here . 这里记载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM