本文介绍了HttpClientBuilder缺少在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apache在pcated DefaultHttpClient德$ P $,但似乎这不是为Android的情况下,也见这里的

导入

  org.apache.httpcomponents:HttpClient的:4.3.5
 

而不是

 新DefaultHttpClient();
 

我现在会用

 的HttpClient的HttpClient = HttpClientBuilder.create()建立()。
 

要创建一个HTTP客户端。这工作正常,在Java项目,但在Android项目中使用的时候,下面的导入缺少

 进口org.apache.http.impl.client.HttpClientBuilder;
 

  HttpClient的sendClient =新DefaultHttpClient();
 

没有被标记为pcated Android中去$ P $和编译罚款。

我不希望添加的Apache HttpClient的第二次(如果我这样做,Android的工作室就无论如何它排除在外)。

Android的文件说,在这里http://developer.android.com/reference/android/net/http/AndroidHttpClient.html#newInstance(java.lang.String)使用

  AndroidHttpClient.new实例(串)
 

获得HTTP客户端与合理的默认值。

有谁知道什么是创建一个HttpClient的在Android上的正确途径,这将是的userAgent字符串!?

解决方案

谷歌Android附带了一个非常过时的(pre-BETA)版本的Apache的HttpClient 4.0。

如果你想使用较新的Apache的HttpClient的API与Android,你应该考虑使用的。

Apache has deprecated DefaultHttpClient, but it seems this is not the case for Android, see also here Java HttpClient - How hard can it be?

Importing

org.apache.httpcomponents:httpclient:4.3.5

instead of

new DefaultHttpClient();

I would now use

HttpClient httpClient = HttpClientBuilder.create().build();

to create an http client. This works fine in a Java project, but when used in an Android project, the following import is missing

import org.apache.http.impl.client.HttpClientBuilder;

while

HttpClient sendClient =  new DefaultHttpClient();

is not marked as deprecated in Android and compiles fine.

I don't want to add the Apache httpclient a second time (and if I would do so, Android Studio would exclude it anyhow).

The Android documentation says here http://developer.android.com/reference/android/net/http/AndroidHttpClient.html#newInstance(java.lang.String) to use

AndroidHttpClient.new Instance(string)

to "get an http client with reasonable defaults".

Does anybody know what is the correct way of creating an HttpClient on Android, and what would be the userAgent string!?

解决方案

Google Android ships with an extremely outdated (pre-BETA) version of Apache HttpClient 4.0.

If you want to use newer Apache HttpClient APIs with Android you should consider using the official Android port.

这篇关于HttpClientBuilder缺少在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:25