【发布时间】:2018-05-28 08:26:38
【问题描述】:
我有一个与 httpclient 4.0.3 版本捆绑在一起的商业应用程序的扩展。如果我尝试使用更高版本,则扩展不起作用。如果我在我的代码中使用 4.0.3 版,它会这样做。我希望关闭 HttpClient 但找不到执行此操作的机制。
我的顶级类是 SwingWorker,它根据访问的 URL 由各种子类扩展。
public class GetPrices extends SwingWorker<List<StockPrice>,Integer> {
private HttpClient client = new DefaultHttpClient();
List<StockPrice> lstStock;
MRBDebug objDebug = MRBDebug.getInstance();
public GetPrices (final List<StockPrice> lstStockp) {
lstStock = lstStockp;
}
@Override
protected List<StockPrice> doInBackground() throws Exception {
int iValue = lstStock.size();
int iProgress = 0;
int iPrevious = 0;
for (StockPrice spLine : lstStock) {
try {
GetPrices.failIfInterrupted();
getData(client, spLine);
iProgress++;
}
catch (IOException a) {
iProgress++;
}
catch (InterruptedException a) {
iProgress++;
}
int iTemp =iProgress * 100 / iValue;
setProgress( iTemp);
}
return lstStock;
}
// Can safely update the GUI from this method.
@Override
protected void done() {
List<StockPrice> spListp;
try {
// Retrieve the return value of doInBackground.
lstStock = get();
setProgress(100);
} catch (InterruptedException e) {
// This is thrown if the thread's interrupted.
} catch (ExecutionException e) {
// This is thrown if we throw an exception
// from doInBackground.
}
}
其中一个子类是:
public class GetYahooPrices extends GetPrices{
private MRBDebug objDebug;
public GetYahooPrices (List<StockPrice> lstStock) {
super(lstStock);
}
@Override
public void getData(HttpClient client, StockPrice spPrice) throws IOException {
objDebug = MRBDebug.getInstance();
String strStock = spPrice.getTicker();
try {
URI uri = new URL("https://finance.yahoo.com/quote/" + strStock + "?p=" + strStock).toURI();
HttpGet httpGet = new HttpGet(uri);
HttpResponse httpResponse = client.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
InputStream stream = entity.getContent();
我希望在完成对 URL 的所有调用后关闭 HttpCLient,但在 4.0.3 版中找不到任何执行此操作的内容。谁能指出我正确的方向?
【问题讨论】:
标签: java windows macos apache http