Reputation: 4816
I am trying to set up my app to throw a timeout exception after 3 seconds on the HttpPost. I am performing this request from an ASyncTask. For some reason, even when I give it a domain that doesnt exist, it hangs for about a minute or two and then throws the last exception. How come my Timeout Exceptions are not working?
protected Void doInBackground(String... params) {
HttpPost httpPost = new HttpPost("http://hgfdhgfdhgfdhfgdhgfdhgfdhfgd.com");
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 3000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch(ConnectTimeoutException e){
Log.e("Timeout Exception: ", e.toString());
} catch(SocketTimeoutException ste){
Log.e("Timeout Exception: ", ste.toString());
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
Upvotes: 1
Views: 4859
Reputation: 3936
Don't call finish there.
Why don't you return null when there is an exception thrown with a particular error message.
In the postexecute method, do whatever needs to be done when the result is null.
Upvotes: 1