Sometimes, although you add <uses-permission android:name="android.permission.INTERNET" />
in the AndroidManifest and you have a WiFi connection, this exception can be thrown. In my case, I have turned off WiFi and then turned it on again. This resolved the error. Weird solution, but sometimes it works.