{"id":2182,"date":"2022-08-30T15:22:44","date_gmt":"2022-08-30T15:22:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/how-to-handle-http-timeout-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:44","modified_gmt":"2022-08-30T15:22:44","slug":"how-to-handle-http-timeout-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-handle-http-timeout-collection-of-common-programming-errors\/","title":{"rendered":"How to handle HTTP timeout?-Collection of common programming errors"},"content":{"rendered":"<p>In my application, I am downloading JSON data from a ReST web service. Most of the time, this works fine, however sometimes the connection will time out.<\/p>\n<p>This is the code I use to set the timeout&#8230;<\/p>\n<pre><code>HttpConnectionParams.setConnectionTimeout( httpParameters, 20000 );\nHttpConnectionParams.setSoTimeout( httpParameters, 42000 );\n<\/code><\/pre>\n<p>If the connection times out, the application crashes and closes, how do I handle a time out?<\/p>\n<ol>\n<li>\n<p>The HttpClient class throws a ConnectTimeoutException Exception, so you should listen for it:<\/p>\n<pre><code>try {\n        HttpResponse response = client.execute(post);\n                    \/\/ do something with response\n    } catch (ConnectTimeoutException e) {\n        Log.e(TAG, \"Timeout\", e);\n    } catch (SocketTimeoutException e) {\n        Log.e(TAG, \" Socket timeout\", e);\n    }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Increase your time of waiting for response like :<\/p>\n<pre><code>HttpConnectionParams.setConnectionTimeout( httpParameters, 60000 ); \/\/1 minute\nHttpConnectionParams.setSoTimeout( httpParameters, 90000 ); \/\/ 1.5 minute\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I have tried to catch a variety of exception types, I have found that catching an IOException worked as I wanted!<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-04 02:52:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In my application, I am downloading JSON data from a ReST web service. Most of the time, this works fine, however sometimes the connection will time out. This is the code I use to set the timeout&#8230; HttpConnectionParams.setConnectionTimeout( httpParameters, 20000 ); HttpConnectionParams.setSoTimeout( httpParameters, 42000 ); If the connection times out, the application crashes and closes, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2182","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2182"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2182\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}