java: throw exception or error if a network call takes too long?
<p>i have developed a method that makes a network http request and waits for the response. i now want to handle a scenario where if the http response takes longer then expected, i want to cancel/terminate that connectinn by throwing some kind of exception plus an error.</p>

<p>im not asking how to throw exceptions and errors, i know that, im just asking how i can some how time how long its taking for me to retrieve a http response?</p>

<p>psuedo below is something like this:</p>

<ol>
<li>start some timer</li>
<li>make http request</li>
<li>wait for http request</li>
<li>if response doesnt get retrieved after X, throw error/exception.</li>
</ol>

<p>Their has been cases where when i do make a http request it takes a minute or two before the jvm finally throws an exception but i wish to throw this exception quicker instead of the app freezing and waiting 3-4mins.</p>

<p>Thanks in advance</p>

<p>Im doing this for a android application so either android specific libs or java 5 libs should be sufficiant for an example on how to achieve this. </p>

<p>edit: here is how my http request code looks like using apache libs:</p>

<pre><code>    import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


private HttpClient httpclient;
private HttpGet get;
private HttpPost post;

/**
     * POST request
     */
    private HttpResponseObject PostRequest() {

        //int response = 0;

        String responseBody = "";
        HttpResponseObject response = new HttpResponseObject();

        try {

            Log.d(TAG, "IN POST_REQUEST_METHOD");
            Log.d(TAG, "URL : " + url);

            httpclient = new DefaultHttpClient();



            post = new HttpPost(url);


            ByteArrayEntity entity = new ByteArrayEntity(data.getBytes());
            entity.setChunked(true);

            post.setEntity(new StringEntity(data));

            HttpResponse httpResponse = httpclient.execute(post);

            HttpEntity resEntity = httpResponse.getEntity();


            if (resEntity != null) {
                // indicate that the content of this entity is no longer
                // required
                response.setResponseBody(EntityUtils.toString(resEntity));

                Header[] header = httpResponse.getAllHeaders();

                Log.d(TAG, "httpResponse.getAllHeaders() = " + header[0].getName());
                response.setResponseHeader(httpResponse.getAllHeaders().toString());
                resEntity.consumeContent();
            }

            // release all recources from the httpClient object
            httpclient.getConnectionManager().shutdown();

            response.setResponseCode(httpResponse.getStatusLine().getStatusCode() ) ;

            Log.d(TAG, "responseBody = " + response.getResponseBody());
            Log.d(TAG, "response code = " + response.getResponseCode());
            Log.d(TAG, "response header = " + response.getResponseHeader());

            return response;

        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "exception e  = " + e.toString());
            return null;
        }

    }
</code></pre>
