HttpClient不会在Android Studio中导入

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

package com.mysite.myapp; import org.apache.http.client.HttpClient; public class Whatever { public void headBangingAgainstTheWallExample () { HttpClient client = new DefaultHttpClient(); } } 

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

Cannot resolve symbol HttpClient

Android Studio SDK中是否包含HttpClient ? 即使它不是,我把它添加到我的Gradle构build像这样:

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.0' compile 'org.apache.httpcomponents:httpclient:4.5' } 

有或没有最后一个编译行,错误是一样的。 我错过了什么?

你不得不使用URLConnection或者降级到sdk 22( compile 'com.android.support:appcompat-v7:22.2.0'

如果你需要sdk 23,把它添加到你的gradle:

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

您也可以尝试下载并直接将HttpClient jar包括到您的项目中,或者使用OkHttp代替

HttpClient在API级别22中被弃用,在API级别23中被删除。如果必须的话,仍然可以在API级别23以后使用它,但是最好移动到受支持的方法来处理HTTP。 所以,如果你用23编译,在你的build.gradle中添加:

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

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' ... } 

使用Apache HTTP进行SDK级别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/ } ... } 

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

从Android工作室通知关于gradle更新

模块特定的build.gradle – /app/build.gradle

 android { compileSdkVersion 23 buildToolsVersion "23.0.2" ... useLibrary 'org.apache.http.legacy' ... } 

试试这个为我工作添加这个依赖到你的build.gradle文件

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

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

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

Android – > Project – > app – > libs

,然后把jar子放在那里。

3在build.grale(Mudule:应用程序)添加

 compile fileTree(dir: 'libs', include: ['*.jar']) 

  dependency { } 

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; 

sdk 23中不再支持HttpClient。Android 6.0(API Level 23)版本不再支持Apache HTTP客户端。 你必须使用

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

并在你的依赖中添加下面的代码片段:

//网页服务的最终解决scheme(包括file upload)

 compile('org.apache.httpcomponents:httpmime:4.3.6') { exclude module: 'httpclient' } compile 'org.apache.httpcomponents:httpclient-android:4.3.5' 

在使用“使用MultipartEntity进行file upload”时,它也会对您有所帮助。

在API 22中,它们会被弃用,在API 23中,它们会将它们完全删除,如果您不需要新添加的所有function,则只需使用API​​ 22之前集成的.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 

在v23 sdk中删除了ApacheHttp客户端。 你可以使用HttpURLConnection或者像OkHttp这样的第三方Http客户端。

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

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

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

如果这不起作用,你可以申请以下黑客行为:

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

– 现在在build.gradle文件的依赖关系{}部分添加编译文件('libs / org.apache.http.legacy.jar')。

您可以简单地将其添加到Gradle依赖关系:

 compile "org.apache.httpcomponents:httpcore:4.3.2" 

简单地使用这个: –

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

在sdk 23和23+中不支持HttpClient。

如果您需要使用sdk 23,请将以下代码添加到您的gradle中:

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

它为我工作。 希望对你有用。

你的项目中有哪些API目标? AndroidHttpClient仅适用于API Level 8 <。 请在这里看看

享受你的代码:)

如果你需要sdk 23,把它添加到你的gradle中:

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

另一种方法是,如果你有httpclient.jar文件,那么你可以这样做:

将.jar文件粘贴到项目的“libs文件夹”中。 然后在gradle中添加这行在你的build.gradle(模块:应用程序)

 dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.0.0' compile files('libs/httpcore-4.3.3.jar') } 

在依赖关系下添加这两行

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

然后

 useLibrary 'org.apache.http.legacy' 

在android下

你只需要添加一行

 useLibrary 'org.apache.http.legacy' 

进入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' } 

如前所述, org.apache.http.client.HttpClient不再支持:

SDK(API级别)#23。

你必须使用java.net.HttpURLConnection

如果你想在使用HttpURLConnection时候使你的代码(和生命)更容易,这里有一个这个类的Wrapper ,它可以让你用GETPOSTPUT简单的操作来使用JSON ,比如做一个HTTP PUT

 HttpRequest request = new HttpRequest(API_URL + PATH).addHeader("Content-Type", "application/json"); int httpCode = request.put(new JSONObject().toString()); if (HttpURLConnection.HTTP_OK == httpCode) { response = request.getJSONObjectResponse(); } else { // log error } httpRequest.close() 

随意使用它。

 package com.calculistik.repository; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * <p> * Copyright © 2017, Calculistik . All rights reserved. * <p> * Oracle and Java are registered trademarks of Oracle and/or its * affiliates. Other names may be trademarks of their respective owners. * <p> * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common * Development and Distribution License("CDDL") (collectively, the * "License"). You may not use this file except in compliance with the * License. You can obtain a copy of the License at * https://netbeans.org/cddl-gplv2.html or * nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific * language governing permissions and limitations under the License. * When distributing the software, include this License Header * Notice in each file and include the License file at * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this particular file * as subject to the "Classpath" exception as provided by Oracle in the * GPL Version 2 section of the License file that accompanied this code. If * applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * <p> * Contributor(s): * Created by alejandro tkachuk @aletkachuk * www.calculistik.com */ public class HttpRequest { public static enum Method { POST, PUT, DELETE, GET; } private URL url; private HttpURLConnection connection; private OutputStream outputStream; private HashMap<String, String> params = new HashMap<String, String>(); public HttpRequest(String url) throws IOException { this.url = new URL(url); connection = (HttpURLConnection) this.url.openConnection(); } public int get() throws IOException { return this.send(); } public int post(String data) throws IOException { connection.setDoInput(true); connection.setRequestMethod(Method.POST.toString()); connection.setDoOutput(true); outputStream = connection.getOutputStream(); this.sendData(data); return this.send(); } public int post() throws IOException { connection.setDoInput(true); connection.setRequestMethod(Method.POST.toString()); connection.setDoOutput(true); outputStream = connection.getOutputStream(); return this.send(); } public int put(String data) throws IOException { connection.setDoInput(true); connection.setRequestMethod(Method.PUT.toString()); connection.setDoOutput(true); outputStream = connection.getOutputStream(); this.sendData(data); return this.send(); } public int put() throws IOException { connection.setDoInput(true); connection.setRequestMethod(Method.PUT.toString()); connection.setDoOutput(true); outputStream = connection.getOutputStream(); return this.send(); } public HttpRequest addHeader(String key, String value) { connection.setRequestProperty(key, value); return this; } public HttpRequest addParameter(String key, String value) { this.params.put(key, value); return this; } public JSONObject getJSONObjectResponse() throws JSONException, IOException { return new JSONObject(getStringResponse()); } public JSONArray getJSONArrayResponse() throws JSONException, IOException { return new JSONArray(getStringResponse()); } public String getStringResponse() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); for (String line; (line = br.readLine()) != null; ) response.append(line + "\n"); return response.toString(); } public byte[] getBytesResponse() throws IOException { byte[] buffer = new byte[8192]; InputStream is = connection.getInputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); for (int bytesRead; (bytesRead = is.read(buffer)) >= 0; ) output.write(buffer, 0, bytesRead); return output.toByteArray(); } public void close() { if (null != connection) connection.disconnect(); } private int send() throws IOException { int httpStatusCode = HttpURLConnection.HTTP_BAD_REQUEST; if (!this.params.isEmpty()) { this.sendData(); } httpStatusCode = connection.getResponseCode(); return httpStatusCode; } private void sendData() throws IOException { StringBuilder result = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { result.append((result.length() > 0 ? "&" : "") + entry.getKey() + "=" + entry.getValue());//appends: key=value (for first param) OR &key=value(second and more) } sendData(result.toString()); } private HttpRequest sendData(String query) throws IOException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); writer.write(query); writer.close(); return this; } } 

错误:(30,0)未findGradle DSL方法:'classpath()'可能的原因:

  • 项目“cid”可能使用了不包含该方法的Android Gradle插件版本(例如,在1.1.0中添加了“testCompile”)。 将插件升级到版本2.3.3并同步项目
  • 项目'cid'可能使用不包含该方法的Gradle版本。 打开Gradle包装文件
  • 构build文件可能缺less一个Gradle插件。 应用Gradle插件
  • 我认为取决于你的Android Studio版本,重要的是你更新你的android工作室以及我也变得沮丧了太多人的build议,但没有运气,直到我不得不升级我的Android版本从1.3到1.5,错误消失像魔法。