I've searched all over the web and after reading lot of docs regarding connection timeout exception, the thing I understood is that, preventing SocketTimeoutException
is beyond our limit. One way to effectively handle it is to define a connection timeout and later handle it by using a try-catch
block. Hope this will help anyone in future who are facing the same issue.
HttpUrlConnection conn = (HttpURLConnection) url.openConnection();
//set the timeout in milliseconds
conn.setConnectTimeout(7000);