英文:
What to replace with deprecated `DefaultHttpAsyncClient`?
问题
我正在修改一个现有项目,但是我看到 DefaultAsyncHttpClient 被弃用了。用什么来替换这个被弃用的?
HttpAsyncClient httpclient;
InputStream is = null;
try {
    // TODO: 已被弃用 ??
    httpclient = new DefaultHttpAsyncClient();
    httpclient.start();
    try {
        // 一些代码
        httpclient.shutdown();
    }
    catch(Exception e) {
    }
}
.start() 方法我也无法获取,.shutdown 方法也一样无法获取。
谢谢帮助!
英文:
I'm modifying a existing project but I see that DefaultAsyncHttpClient is deprecated. What to replace the deprecated one?
		HttpAsyncClient httpclient;
	    InputStream is = null;
	    try {
		    // TODO: deprecated ??
		    httpclient = new DefaultHttpAsyncClient();
		    httpclient.start();
        try {
           
            // some code
            httpclient.shutdown();
        }
        
        catch(Exception e) {
         
        }
        }
.start() i cant fetch that method either and neither i can fetch .shutdown method.
Thank for help!
答案1
得分: 0
你可以使用 CloseableHttpAsyncClient,用 close 替代 shutdown
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
httpclient.start();
HttpGet request = new HttpGet("http://httpbin.org/get");
Futurefuture = httpclient.execute(request, null); 
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
System.out.println("Shutting down");
} finally {
httpclient.close();
}
英文:
You can use CloseableHttpAsyncClient , close instead of shutdown
>     CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
>        try {
>            httpclient.start();
>            HttpGet request = new HttpGet("http://httpbin.org/get");
>            Future<HttpResponse> future = httpclient.execute(request, null);
>            HttpResponse response = future.get();
>            System.out.println("Response: " + response.getStatusLine());
>            System.out.println("Shutting down");
>        } finally {
>            httpclient.close();
>        }
专注分享java语言的经验与见解,让所有开发者获益!

评论