How to check if internet connection has been established?

1,121 views
Skip to first unread message

Roman Lazoryshchak

unread,
Nov 30, 2014, 1:56:01 PM11/30/14
to kivy-...@googlegroups.com
Hi! I I have an app which work with internet connection and scraping some data from the web site. When I run an app without establishing internet connection on my android device it is crashed. I would like to ask how check internet connection on android device by using kivy framework. Thanks for any help.

Emil Salib

unread,
Nov 30, 2014, 7:32:06 PM11/30/14
to kivy-...@googlegroups.com
Hello,
You will need to check the status of the WiFi type connection using android connectivitymanager class which can be accessed in python through pyjnius autoclass. 
Best,
Emil
Message has been deleted

Felipe França

unread,
Dec 1, 2014, 2:12:32 PM12/1/14
to kivy-...@googlegroups.com
Try this:

import urllib2

...

try:
    response
=urllib2.urlopen('http://200.252.60.80',timeout=1)
except urllib2.URLError as err:
   
print = "No internet access!"

where '200.252.60.80' is the ip address that you want to connect, or you can put 'http://www.something.com' if you wish.
I hope it helps you.

Roman Lazoryshchak

unread,
Dec 1, 2014, 2:15:39 PM12/1/14
to kivy-...@googlegroups.com
Yep it works! Thank you

понедельник, 1 декабря 2014 г., 21:12:32 UTC+2 пользователь Felipe França написал:

Emil Salib

unread,
Dec 2, 2014, 7:37:29 PM12/2/14
to kivy-...@googlegroups.com

Felipe,
Very clever. 
Emil

Mathieu Virbel

unread,
Dec 4, 2014, 3:46:41 AM12/4/14
to kivy-...@googlegroups.com
Beware of a such code. Anybody with a slow connection will fail, a 1s timeout is not enough. And lock your mainthread, so it must happen in another thread, or use UrlRequest.
Emil approach is more safe: using the Connectiviy manager on android would be the first thing to do.
Then, just handle any error in your workflow on network and in case of an error, analyse it and correctly redirect the user on a good error message.

Checking internet at the start / before doing things doesn't mean you'll be free of error after, so conceptually, the approach is not helping.

Either ask internet connectivy to show an online/offline mode. If the connectivity is available, then do the usual work. 
Reply all
Reply to author
Forward
0 new messages