Rob
Rob

Reputation: 821

HTTP Post completely exiting thread, no exception caught, or hangs forever

I knew my code was dying somehow because of the other symptoms, but even though I had try/catch around everything, there was never any message. I've now finally narrowed it down by use of successive output statements.

    String line = null;
    try {
        final HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 30000);    
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
        HttpPost httpPost = new HttpPost(url);
        postDebugMessage("GlobalApplication", "Info", "Executing httppost");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        postDebugMessage("GlobalApplication", "Info", "Getting entity");
        HttpEntity entity = httpResponse.getEntity();
        postDebugMessage("GlobalApplication", "Info", "Converting to line");
        line = EntityUtils.toString(entity);   //<- the bad one
        postDebugMessage("GlobalApplication", "Info", "Conversion completed.");
    }
    catch (Exception ex) {
        // Any error occurring here we're going to state is
        // a network error.
        postDebugMessage("GlobalApplication", "Exception", ex.getStackTrace()[0].toString());
        throw new NetworkNotAvailableException(ex.toString());

    }

Based on my debug output statements, the line = EntityUtils.toString(entity) never completes, and no statement after that in the entire service thread executes...the whole thing just vanishes.

I know that EntityUtils.toString() can throw 2 exceptions, an IOException and a ParseException, and I'm going to try and add those next, but shouldn't an overall Exception catch it?

Upvotes: 1

Views: 781

Answers (3)

Rob
Rob

Reputation: 821

I'm going to call this answered, because I've solved my problem it appears, but it wasn't related to the EntityUtils call (at least so far as I can tell).

It was not an exception exiting the thread completely, it was just a hung thread. The culprit was that I had put in a connection timeout, but not a socket connection timeout. The docs say that the default value is an infinite timeout...and that's what I was seeing I'd say. I added:

  HttpConnectionParams.setSoTimeout(httpParams, 30000);

And haven't seen the issue since.

I know at least one log I had showed the entityUtils call never finishing, so that's what led me to think it was that, but more logs after that showed it was the .execute(httpPost) that never completed, so that's what made me look there.

Thanks for the help!

Upvotes: 1

fleetway76
fleetway76

Reputation: 1640

Not necessarily. I suspect that you might be getting an unchecked exception that extends Error rather than Exception. Try putting catch( Throwable t) and see if it catches it. You probably dont want to be actually catching these exceptions, as the state of the system afterwards could be unpredictable if its say an out of memory exception.

Upvotes: 0

Kaj
Kaj

Reputation: 10959

EntityUtils.toString(entity) first reads some data, and then it does this:

char[] tmp = new char[1024];
int l;
while((l = reader.read(tmp)) != -1) {
   buffer.append(tmp, 0, l);
}

So the method will block as long as the server is sending more data. I hope that you haven't connected to a streaming service?

Upvotes: 0

Related Questions