1

I am trying to retrieve a text file from the internet using the apache httpclient. I am using the following code:

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

I am getting the following stack trace:

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

I'm not that familiar with debugging, so I'm not sure what's exactly happening.

1
  • 1
    Did you try to download the files sequentially? I am not sure, whether the HttpClient actually supports this interleaved kind of operation shown in your code. Commented Aug 6, 2012 at 14:52

2 Answers 2

2

The response body must be consumed before the connection can be used for another request. You should read the response InputStream fully prior to the next HTTP execute. Your code should be appear in the following order:

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

    responseC = httpclient.execute(getCNMR);
    HttpEntity entityC = responseC.getEntity();
    CNMR = EntityUtils.toString(entityC);
Sign up to request clarification or add additional context in comments.

Comments

1

Is it a requirement that you use httpClient? Given that it is only a text file and you don't need any post or get parameters, a simpler method would be:

URL url = new URL("http://www.hmdb.ca/labm/metabolites/" + HMDB + "/chemical/pred_hnmr_peaklist/" + HMDB + "_peaks.txt");
InputStream response = url.openConnection().getInputStream();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.