自 API 级别 22 起,Android 已弃用 Apache 模块,所以我的问题是,我该如何使用,例如来自 Apache 库而不是 Android SDK 的 HttpResponse
?问题是这两个包都是一样的。
但是,例如 HttpGet
是可以的,因为它在 Apache 中被称为 HttpGetHC4
。
最佳答案
方法 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;
}
我希望这对某人有所帮助。
关于Android 已弃用 apache 模块(HttpClient、HttpResponse 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294479/