As Erel said above, the surest way is to try sending your request and handle it properly if it fails.
The alternatives all have problems, as far as I know:
- Checking for wifi ignores mobile connections.
- Checking for Internet (wifi, mobile, whatever) is better, but you may not be able to reach the desired server (e.g. a ping will fail).
- In some cases, you can ping a server but can't reach it with a real request. (This one is really annoying to me.)