简体   繁体   English

HTTP获取Android Studio API级别23+

[英]HTTP Get in Android Studio API level 23+

Hi! 嗨! I've been searching around on the Internet for how to make Http Get app in Android Studio, what I found was that you could add 我一直在互联网上搜索如何在Android Studio中制作Http Get应用,我发现您可以添加

compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.5'

to the dependencies for Android API level 23>. 到Android API级别23的依赖项>。 Or 要么

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

for Android API level 23+ And then I've been getting the Java code 适用于Android API级别23+,然后我一直在获取Java代码

HttpClient httpClient= HttpClientBuilder.create().build();
HttpGet request= new HttpGet(your_url_to_read);
request.setHeader("k1", "value1");
request.setHeader("k2", "value2");
HttpResponse response= httpClient.execute(request);

System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
response.getStatusLine().getStatusCode());
//want to read the response content?
BufferedReader rd = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
    result.append(line);
}

by @user404. 通过@ user404。 I've successfully been importing the other packages in the Java code below, but on line 1 Android Studio cannot import HttpClientBuilder. 我已经成功地在下面的Java代码中导入了其他软件包,但是在第1行上,Android Studio无法导入HttpClientBuilder。 Then I came in on a thread about HttpClientBuilder in Android API level 23+ (Which I forget where it was, but it was somewhere on StackOverflow) there someone was saying that you can not import HttpClietBuilder in Android API 23+ because Google removed that feature. 然后,我进入了一个关于Android API级别23+中的HttpClientBuilder的线程(我忘记了它的位置,但是它位于StackOverflow上的某个地方) ,有人说您无法在Android API 23+中导入HttpClietBuilder,因为Google删除了该功能。

So now I'm looking either for a method to import that HttpClientBuilder package without downgrading the API level or a whole new way to make an Http Get request in Java with headers, like "URL " http://www.something.com/random " HEADERS "Authorization": "123456ABCDEF"" , not parameters in the URL like " http://www.something.com/random?authorization=123456ABCDEF ". 因此,现在我正在寻找一种在不降低API级别的情况下导入该HttpClientBuilder包的方法,或者一种全新的方式来在Java中通过标头(例如“ URL” http://www.something.com/)发出Http Get请求。随机 “磁头‘授权’:‘123456ABCDEF’”,而不是在URL参数,如“ http://www.something.com/random?authorization=123456ABCDEF ”。

Thanks in forehand for answer 正手感谢您的回答

It's just that the org.apache.http packages have been removed by default, so if you target API level 23, you'll need to do something extra to make these work. 只是默认情况下已删除org.apache.http软件包,因此,如果您以API级别23为目标,则需要做一些额外的事情才能使这些工作。

Adding useLibrary 'org.apache.http.legacy' to your Gradle is a way to fix this and you'll be able to target 23. However, you'll get the old implementation which is legacy for a reason. 在您的Gradle中添加useLibrary 'org.apache.http.legacy'是解决此问题的一种方法,您将可以定位到23。但是,您将获得旧的实现,这是有原因的。

Manually adding the latest version of Apache HttpClient as a dependency is a good choice and will continue to work regardless of which Android version you're targeting. 手动添加最新版本的Apache HttpClient作为依赖项是一个不错的选择,无论您要定位的是哪个Android版本,它都可以继续工作。

Likewise, you can use another HTTP library altogether like OkHttp. 同样,您可以一起使用另一个HTTP库,例如OkHttp。 You can also use a library which is more higher level, like Retrofit. 您也可以使用更高级别的库,例如Retrofit。

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

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