从API level 22开始,Android就弃用了Apache模块,所以我的问题是,我该如何使用Apache库中的HttpResponse
,而不是Android SDK?问题是这两个包都是一样的。
但是,例如,HttpGet
是可以的,因为它在Apache中被称为HttpGetHC4
。
发布于 2015-04-27 14:58:51
方法HttpClient已弃用。您现在可以使用URLConnection,如本例所示:
private StringBuffer request(String urlString) {
// TODO Auto-generated method stub
StringBuffer chaine = new StringBuffer("");
try{
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
chaine.append(line);
}
}
catch (IOException e) {
// Writing exception to log
e.printStackTrace();
}
return chaine;
}
我希望这对某些人有帮助。
发布于 2016-06-04 04:40:38
使用Apache的模块并没有什么坏处。谷歌把事情搞得一团糟,因为他们没能做出一个成功的分支。Google和Apache的集成是由Jesse Wilson监督的-他在Google工作,搞乱了一切,然后在square工作期间制作了自己的库(OkHttp)。这真是个丑陋的故事。
我建议不要使用遗留的JAR文件,因为它包含没有改进和错误修复的旧版本的Apache库(它是一个very old pre-BETA snapshot)。正如你在Apache组件的official page上看到的,有一个新的4.4版本,与所有Android版本兼容。它只需要在不同的命名空间下重新打包,以避免与旧版本发生冲突。
您可以简单地从Maven (或download release from GitHub)添加依赖项:
dependencies {
compile "cz.msebera.android:httpclient:4.4.1.2"
}
然后用cz.msebera.android.httpclient
替换org.apache.http
,这样您的导入将如下所示:
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpHost;
import cz.msebera.android.httpclient.HttpResponse;
是的,您可以继续使用Apache库,而不会浪费时间重写工作代码!
至于使用Apache组件和HttpURLConnection之间的区别,HttpURLConnection使用响应缓存...仅此而已。我不确定你是否真的想要它,而且你总是可以自己实现它。
顺便说一句,我尝试过像HttpURLConnection这样的替代方案--它们甚至都比不上阿帕奇的功能和简单性。
发布于 2015-08-28 12:36:13
在Android Marshmallow (sdk 23)中,您可以添加:
useLibrary 'org.apache.http.legacy'
在android {}
部分中使用build.gradle作为解决方法。这对于Google自己的gms库来说似乎是必要的!
https://stackoverflow.com/questions/29294479
复制相似问题