gpt4 book ai didi

java - IllegalStateException java httpclient

转载 作者:行者123 更新时间:2023-11-29 05:56:42 33 4
gpt4 key购买 nike

我正在尝试使用 apache httpclient 从 Internet 检索文本文件。我正在使用以下代码:

        HttpClient httpclient = new DefaultHttpClient();
HttpGet getHNMR = new HttpGet("http://www.hmdb.ca/labm/metabolites/" + HMDB + "/chemical/pred_hnmr_peaklist/" + HMDB + "_peaks.txt");
HttpGet getCNMR = new HttpGet("http://www.hmdb.ca/labm/metabolites/" + HMDB + "/chemical/pred_cnmr_peaklist/" + HMDB + "_peaks.txt");

try {
responseH = httpclient.execute(getHNMR);
responseC = httpclient.execute(getCNMR);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("client exception");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("ioexception");
}
//Generate HNMR peak list
HttpEntity entityH = responseH.getEntity();
HttpEntity entityC = responseH.getEntity();;
try {
HNMR = EntityUtils.toString(entityH);
CNMR = EntityUtils.toString(entityC);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("parseexception");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("ioexception");
}
//Set peak lists to textarea
HC.setText(CNMR + "\n" + HNMR);

我得到以下堆栈跟踪:

Thread [pool-2-thread-1] (Suspended (exception IllegalStateException))  
ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: 1128
ThreadPoolExecutor$Worker.run() line: 603
Thread.run() line: 679

我不太熟悉调试,所以我不确定到底发生了什么。

最佳答案

必须在连接可用于另一个请求之前使用响应正文。您应该在下一次 HTTP 执行之前完整阅读响应 InputStream。您的代码应按以下顺序出现:

    responseH = httpclient.execute(getHNMR);
HttpEntity entityH = responseH.getEntity();
HNMR = EntityUtils.toString(entityH);

responseC = httpclient.execute(getCNMR);
HttpEntity entityC = responseC.getEntity();
CNMR = EntityUtils.toString(entityC);

关于java - IllegalStateException java httpclient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830090/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com