简体   繁体   English

在Java中我遇到错误无法解析符号HttpClient,我该如何解决?

[英]In java i'm getting error cannot resolve symbol HttpClient how can i fix it?

In android studio in mainactivity at the top I have the lines 在顶部的mainactivity的android studio中,我有台词

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EncodingUtils;

Cannot resolve symbol of HttpClient HttpPost and EncodingUtils All this marked in red. 无法解析HttpClient的符号HttpPost和EncodingUtils所有这些都标记为红色。

If I remember right my project when created it was targeting api 15. My device to run it later on is lg g3. 如果我没记错,我创建的项目针对的是api15。我稍后运行该设备的设备是lg g3。

Also I installed android studio yesterday and inside the android studio I didn't checked and installed the AVD manager packages not sure if I should or they are already installed. 我昨天还安装了android studio,在android studio中没有检查并安装了AVD Manager软件包,不确定是否应该安装或已经安装了它们。

This is what I see when going to AVD I see there also something Failed to load on the right top corner. 这是我在转到AVD时看到的内容,在右上角也看到无法加载的内容。

AVD

You need to add this inside Build.Gradle android . 您需要在Build.Gradle android添加它。

android {
    compileSdkVersion 23
    buildToolsVersion '22.0.1'

    useLibrary 'org.apache.http.legacy'
}

Also add this to Dependencies . 还将其添加到Dependencies

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

Apache HttpClient has been deprecated and removed from Android 6 onwards as per this announcement . 根据此公告, Apache HttpClient已被弃用,并已从Android 6开始删除。 You should not be using it. 您不应该使用它。

The recommended way now is to use HttpUrlConnection . 现在推荐的方法是使用HttpUrlConnection

OR, switch to OkHttp . 或者,切换到OkHttp

Both of these are said to be much efficient than the legacy HttpClient. 据说这两者都比旧版HttpClient高效。

As android removed apache libraries , you need to add those libraries yourself 由于android删除了apache库,因此您需要自己添加这些库

to add follow these steps 添加请按照以下步骤

1 ) download Apache HttpClient and HttpCore jar files (you can get here link ) 1)下载Apache HttpClient和HttpCore jar文件(您可以在此处链接

2) copy the jar files and paste it in libs folder of your application. 2)复制jar文件并将其粘贴到应用程序的libs文件夹中。 3) right click on your app folder and open module setting.select dependencies tab 3)右键单击您的应用程序文件夹并打开模块设置。选择依赖项选项卡

4)click green + button and selectFile dependency and then select your jar file. 4)单击绿色+按钮并选择文件依赖性,然后选择您的jar文件。 and click on dropdown and select that as "Compile" 然后点击下拉菜单,然后选择“编译”

5)if you get any error while building gradle.(Duplicate files Exception) 5)如果在构建gradle时遇到任何错误。(重复文件异常)

add this lines to build 添加此行以构建

packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
}

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

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