gpt4 book ai didi

Android 无法访问 org.apache.http.client.HttpClient

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:59 26 4
gpt4 key购买 nike

我正在使用 android studio 创建一个向服务器发出 GET 请求的应用程序。我的代码是这样的:

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;
}
}
}

问题是 Android Studio 标记了行

client.execute(get);

出现错误:

enter image description here

说“无法访问 org.apache.http.client.HttpClient”

这是我的 gradle 文件:

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'

}

最佳答案

在 Android SDK 23 中

HttpClient 已弃用,因为它会推断,您可以在 HttpURLConnection 中迁移代码

https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

你可以用这个,但不推荐了

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

对于 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32960599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com