Android弃用了apache模块(HttpClient,HttpResponse等)

Android弃用了apache模块(HttpClient,HttpResponse等)

自API级别22以来,Android已经弃用了Apache模块,所以我的问题是,我如何使用,例如  HttpResponse来自Apache库,而不是Android SDK?问题是两个包都是一样的。

但是,例如,HttpGet没关系,因为它HttpGetHC4在Apache中被调用。


慕桂英4014372
浏览 341回答 3
3回答

翻翻过去那场雪

在Android Marshmallow(sdk 23)中,您可以添加:useLibrary 'org.apache.http.legacy'在该android {}部分中build.gradle 作为变通方法。这似乎是谷歌自己的一些gms库所必需的!

哆啦的时光机

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;}我希望这能帮助别人。
打开App,查看更多内容
随时随地看视频慕课网APP