问题描述
我使用的是Android的工作室创建一个应用程序,使一个GET请求到服务器。我的code是这样的:
进口org.apache.http.client.methods.CloseableHtt presponse;
进口org.apache.http.client.methods.HttpGetHC4;
进口org.apache.http.impl.client.CloseableHttpClient;
进口org.apache.http.impl.client.HttpClientBuilder;
公共类HTTPHelper
{
私人字符串基地;
公共HTTPHelper(字符串基地)
{
this.base =基地;
}
公共字符串的doGet(字符串路径)
{
尝试
{
CloseableHttpClient客户端= HttpClientBuilder.create()建立()。
HttpGetHC4 GET =新HttpGetHC4(路径);
CloseableHtt presponse响应= client.execute(获得);
返回 ;
}
赶上(例外五)
{
返回null;
}
}
}
该proiblem是,Android的工作室,标志着该行
client.execute(获得);
有一个错误:
说无法访问org.apache.http.client.HttpClient
下面是我的摇篮文件:
应用插件:com.android.application
安卓{
compileSdkVersion 23
buildToolsVersion23.0.0
defaultConfig {
的applicationIDprincipal.halloween
的minSdkVersion 16
targetSdkVersion 23
版本code 1
VERSIONNAME1.0
}
buildTypes {
推出 {
minifyEnabled假
proguardFiles getDefaultProguardFile('ProGuard的-android.txt'),'proguard-rules.pro
}
}
}
依赖{
编译文件树(导演:库,包括:['的* .jar'])
编译com.android.support:appcompat-v7:23.0.0
编译com.pubnub:pubnub-安卓3.7.5
编译com.google code.gson:GSON:2.3.1
编译组:org.apache.httpcomponents,名称:HttpClient的,Android的版本:4.3.5.1
编译org.apache.httpcomponents:HttpClient的:4.3.6
}
在Android SDK中23
HttpClient的是pcated,因为它的推理,就可以迁移code。在的HttpURLConnection
https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client
您可以利用这一点,但我们不推荐了。
安卓{
useLibraryorg.apache.http.legacy
}
对于的HttpURLConnection
网址URL =新的URL(urlString);
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
connection.setRequestMethod(GET);
connection.setDoInput(真正的);
connection.connect();
I'm using android studio to create an app that makes a GET request to a server. My code is this:
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGetHC4;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class HTTPHelper
{
private String base;
public HTTPHelper (String base)
{
this.base = base;
}
public String doGet (String path)
{
try
{
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGetHC4 get = new HttpGetHC4(path);
CloseableHttpResponse response = client.execute(get);
return "";
}
catch (Exception e)
{
return null;
}
}
}
The proiblem is that Android Studio marks the line
client.execute(get);
with an error:
saying "Cannot access org.apache.http.client.HttpClient"
Here's my gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "principal.halloween"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.pubnub:pubnub-android:3.7.5'
compile 'com.google.code.gson:gson:2.3.1'
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
compile 'org.apache.httpcomponents:httpclient:4.3.6'
}
In Android SDK 23
HttpClient is deprecated because it inference, you can migrate your code in HttpURLConnection
You can use this, but it's not recommended anymore
android {
useLibrary 'org.apache.http.legacy'
}
For the HttpURLConnection
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
这篇关于Android无法访问org.apache.http.client.HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!