且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android 弃用 apache 模块(HttpClient、HttpResponse 等)

更新时间:2023-02-16 20:39:18

不推荐使用 HttpClient 方法.您现在可以使用 URLConnection,如本示例中所示:

The method HttpClient was deprecated. You can now use the URLConnection as you can see in this example:

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;
}

我希望这对某人有所帮助.

I hope this is helping someone.