Android Question Testing Internet Connection

Cableguy

Expert
Licensed User
Longtime User
Hi... huh... It's me ... huh... again... huh.. I guess...

I've been searching the forum for an easy, bulletproof way of testing if an internet connection is available or not...

Saw a post that based it on a request to a server in order to retrieve it's own IP... not a bad solution but I dont have and don't want to implement my own server just for this...
Saw another that based it on a ping request... but it would block the main thread as long as the ping request was running...
Back in the Very Old Days of windows mobile, we had within the WebBrowser lib, a method that would return true if a page was accessible, like google or b4ppc... can't remember the exact name of the method dough...
Found the snipet in B4ppc
So, being that, most of the Android world apps use internet, and that low-end and mid-en devices only have WI-FI capabilities, what is the best way to go to test if an internet connection is possible?
 
Last edited:

TheMightySwe

Active Member
Licensed User
Longtime User
Actually Microsoft do just that? Have you noticed that there is even software easily accessable down in the right bottom corner on the screen for error elimination on the user side. It's called Windows Network Diagnostics. But maybe that is to basic for a "Professional Server Manager". But for us that are not "Professional Server managers" its a pretty good tool to see where the problem is.

detecting-problems.png


I'm sorry, but it's not your call to decide what is and what is the best way to handle our customers network problems. It's all about convienience for us, our customers and to find the source of the problem in the fastest simplest way. Users are not always aware of the capabilities of the Android OS, I want a simple way for them to check these things. If it does not suit you in your business, so be it, i can respect that. But in our business it works just fine.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Actually Microsoft do just that? Have you noticed that there is even software easily accessable down in the right bottom corner on the screen for error elimination on the user side. It's called Windows Network Diagnostics. But maybe that is to basic for a "Professional Server Manager". But for us that are not "Professional Server managers" its a pretty good tool to see where the problem is.

detecting-problems.png


I'm sorry, but it's not your call to decide what is and what is the best way to handle our customers network problems. It's all about convienience for us, our customers and to find the source of the problem in the fastest simplest way. Users are not always aware of the capabilities of the Android OS, I want a simple way for them to check these things. If it does not suit you in your business, so be it, i can respect that. But in our business it works just fine.
Calm down! I don't say that my practice is the best one on Earth. That would be stupid. We all do what we think is the best for our job. I just try to make things clear and your reasons are not. I can't see why you need to reach a DNS. If you have a japanese customer that cannot access to your DNS for an unknown reason but have a perfect access to the server used by your app, what's your conclusion? What your app will do? I really don't understand. Microsoft doesn't do it, Google doesn't do it, Facebook doesn't do it, so if I had to answer with the same agressive tone that you use, I would ask: why do you think you're smarter than them? And you probably noticed that I did not talk of the diagnosis tools that these companies may provide because, of course, the main goal of these tools is to provide clues about what's going wrong. I talked of the connected apps of these companies, like your Play Store or your Internet Browser. If we cannot discuss and exchange ideas, if you cannot explain in simple words what's the advantage of your solution, then we should stop now. I don't like to be taken for a stupid guy.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
We don't want to know if we are connected to google or not, all we want to know is if a connection is at all possible.
Why? To save time to the user...
My scenario is, I will try to tap into a browser game...
This game in particular has a login page that can be accessed either by the root web address or by "/login.php". So before I try to connect to the server and wait for a timeout error or an impossible to parse page, I would prefer to be sure that a connection is possible, hardware wise, so that the info can be downloaded in the minor amount of time possible.
There's a thing I don't understand in your answer. You say "before I try to connect to the server", so with what server did you check the connection?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
In case someone else would feel offended by my comments (I don't really see why but...) I'd like to clarify the situation. My job is to manage 400 high-availability servers so I necessarily have a professional point of view on the subject. As I practice this job, I do not need information coming from the client side. By saying that, I never wanted to say it was the only way or the best way to manage servers. It's just to testify. In other situations than mine, or with other means than mine, there are other practices. To speak frankly, I almost implemented too a little test to Google.com in my Google Play Game Services library, but I realized it was useless. I did not see what I could learn from it. So is there a reason why so many people do that?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
All I am interested in my applications is:
1. is Internet available and am I connected and on line?
If so, I either upload files from the device or download files to the device from an FTP or DropBox for instance.
I have seen different ways on the forum. Can someone give us a short half dozen lines of code with the simplest and surest way to check if I am on line and my upload/download will be successful. That is as simple as that.
Thank you guys for reviving the subject.
 
Upvote 0

TheMightySwe

Active Member
Licensed User
Longtime User
Calm down! I don't say that my practice is the best one on Earth. That would be stupid. We all do what we think is the best for our job. I just try to make things clear and your reasons are not. I can't see why you need to reach a DNS. If you have a japanese customer that cannot access to your DNS for an unknown reason but have a perfect access to the server used by your app, what's your conclusion? What your app will do? I really don't understand. Microsoft doesn't do it, Google doesn't do it, Facebook doesn't do it, so if I had to answer with the same agressive tone that you use, I would ask: why do you think you're smarter than them? And you probably noticed that I did not talk of the diagnosis tools that these companies may provide because, of course, the main goal of these tools is to provide clues about what's going wrong. I talked of the connected apps of these companies, like your Play Store or your Internet Browser. If we cannot discuss and exchange ideas, if you cannot explain in simple words what's the advantage of your solution, then we should stop now. I don't like to be taken for a stupid guy.

Well, that makes two of us.

And I do not imply that you are stupid, i know you are not. I just trying to explain the scenarios where testing the connection status kan be good practice.

They shall ofcourse not access my DNS, they need to have a DNS availble for the name translation. As the other guy pointed out, it's good to know if you have a basic connection availible before you try to connect something with HTTP or other protocols, because they have a timeouts and that can be very frustrating for customers to get cryptic error messages from these protocols. That's all.

And personally i like to say to a customer to run this test that i have availible easy in a menu so I can know if there is a more serious issue than the usual router crash or hang that is the problem 99% of the times before I send someone to check the hardware.

My tests are and this is not only adressed to you Informatix, I know you already know this but more to explain why I have the functionallity we are discussing here.

First I check if the WIFI or mobile connection is actually on, and turn them on if they are not.

There is a few Android battery saving functions that turn of these of save power without any info to the user, so when they come back from Pause all of the sudden the WIFI or the mobile is off. I tell the customers to charge if the battery is low.

Checking if connected

Then I check if the WIFI or mobile connection is connected. If not I ask them to move if they are on mobile connection.

Then I check the ping to our gateway

The reason for this is that some mobile and other routers goes into power saving mode and close the connection if they are inactive to long. So if that fails wait 5 seconds and send it again, usually the router takes a few seconds to wake up and connect again. I here show a toast message to inform the user that connection is temporary down, trying again in 5 4 3 2 1 seconds.

If this keeps failing 6 times i ask the customer to restart the router.

If that fails even if the router is restarted i do the DNS check, because you can have a connection to the WIFI router without actually be abel to connect to the internet.

I ask them to check the WIFI so it's not moved to a unprefered open connection in the neighbourhood. (That happens sometimes). Then I ask them to "forget" that WIFI-network and connect to the right network.

If that is not working I ask the customer to check if any cables is disconnected. Then I ask the customer to please call the ISP and see if they have any problems.

When all this is checked, i put up the number to us, because then the 99.99% of the external error sources are eliminated, then we can start checking if there is any errors or service crashes on our side. One time in about 2 year that has happend, the software on our side had crashed and I had forgot to check the restart at crash in the server.

Thats how we do, and it works for us. Of course it might be better ways to do it, but the persons on the other side is not tech wizards, and they need simple instructions to eliminate the most common errors.

I hope you understand my reasoning here, and if you see anything I can do better i'm open to suggestions.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Well, that makes two of us.

And I do not imply that you are stupid, i know you are not. I just trying to explain the scenarios where testing the connection status kan be good practice.

They shall ofcourse not access my DNS, they need to have a DNS availble for the name translation. As the other guy pointed out, it's good to know if you have a basic connection availible before you try to connect something with HTTP or other protocols, because they have a timeouts and that can be very frustrating for customers to get cryptic error messages from these protocols. That's all.

And personally i like to say to a customer to run this test that i have availible easy in a menu so I can know if there is a more serious issue than the usual router crash or hang that is the problem 99% of the times before I send someone to check the hardware.

My tests are and this is not only adressed to you Informatix, I know you already know this but more to explain why I have the functionallity we are discussing here.

First I check if the WIFI or mobile connection is actually on, and turn them on if they are not.

There is a few Android battery saving functions that turn of these of save power without any info to the user, so when they come back from Pause all of the sudden the WIFI or the mobile is off. I tell the customers to charge if the battery is low.

Checking if connected

Then I check if the WIFI or mobile connection is connected. If not I ask them to move if they are on mobile connection.

Then I check the ping to our gateway

The reason for this is that some mobile and other routers goes into power saving mode and close the connection if they are inactive to long. So if that fails wait 5 seconds and send it again, usually the router takes a few seconds to wake up and connect again. I here show a toast message to inform the user that connection is temporary down, trying again in 5 4 3 2 1 seconds.

If this keeps failing 6 times i ask the customer to restart the router.

If that fails even if the router is restarted i do the DNS check, because you can have a connection to the WIFI router without actually be abel to connect to the internet.

I ask them to check the WIFI so it's not moved to a unprefered open connection in the neighbourhood. (That happens sometimes). Then I ask them to "forget" that WIFI-network and connect to the right network.

If that is not working I ask the customer to check if any cables is disconnected. Then I ask the customer to please call the ISP and see if they have any problems.

When all this is checked, i put up the number to us, because then the 99.99% of the external error sources are eliminated, then we can start checking if there is any errors or service crashes on our side. One time in about 2 year that has happend, the software on our side had crashed and I had forgot to check the restart at crash in the server.

Thats how we do, and it works for us. Of course it might be better ways to do it, but the persons on the other side is not tech wizards, and they need simple instructions to eliminate the most common errors.

I hope you understand my reasoning here, and if you see anything I can do better i'm open to suggestions.
Yes, I understand your reasoning. It's a typical business case, where customers are known and can be contacted by phone. I'm in the same case (with a different practice as you have understood). I was thinking of a more general case for Android, with numerous users who are anonymous and cannot be called to be helped (and developpers here are probably more in this case). For example, you create an app distributed on the PlayStore that retrieves data from internet. You have to contact a server for that. And many people ask: before trying to connect to this server, I want to know whether there's an Internet connection available. But there's a problem to solve: what method do I use? It's always a bit strange to me to try to test a server that is not the server to reach. Suppose that it does not answer because it is down, or because your internet provider blocked the access to this server (in some countries, Google or Youtube cannot be accessed) or any other uncommon reason (but an uncommon case always happens some day). If you decide that you're not connected because of that, then you make a mistake. And if you know how to test a server, why don't you test your own server? That really puzzles me. Suppose that you succeed in contacting Google.com (or any other pool of servers that answers with a rate close to 100%). You think you're connected (and you're right) so you continue to the next step: establishing a connection to your own server. But it doesn't answer for any reason. What's the usefulness of the first connection check? The answer usually is: if my connection to Google fails, then it's useless to waste my time trying to connect to my server because there's a timeout, etc. so the first connection check is there to gain time. Okay, but if you're able to test quickly whether Google.com is available or not, why don't you do the same thing with your own server? And I didn't even discuss of the case where your server is temporarily down (it's rebooting for example). That's why I understand why Google does only a single check to the real server and displays only "unable to connect, retry later" in case of failure.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
... I would prefer to be sure that a connection is possible, hardware wise...
As I said... If at least we are sure that hardware wise all is good, then we can try to connect to whatever server we need or choose...

Guys, this was a simple question on "how can I achieve this", no need to start a philosophical battle over WHY or if it's indeed useful or not.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
And if you know how to test a server, why don't you test your own server?
Because redundancy.
Suppose you test your own server, and for whatever connectivity reason, it doesnt connect. But you show a message to the user saying 'Your internet is not connected', that will annoy him because he KNOWs his connection is fine and he can connect to other sites. You just dont want to give the wrong error message.
I had this problem with CP showing the wrong error message on a bad response from Dropbox (due to invalid charachters in their filenames), and saying there was an internet connectivity problem.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Because redundancy.
Suppose you test your own server, and for whatever connectivity reason, it doesnt connect. But you show a message to the user saying 'Your internet is not connected',

We agree that wouldn't be the right message to display. The Play Store app displays "check the connection and retry" (my translation) with a Retry button, whatever problem you may have. But I prefer: "Unable to connect", seen in the Amazon Store app, with a Retry button. The user cannot do nothing for most connection failures and what he can do (e.g. turning the Wifi on) can be suggested after calling the functions of the NetworkInfo class, without connecting to any server. Google had written a lesson on this matter.

People want answers in this forum on how to check a connection. Ok, here's one answer: in a separate thread (because all network operations under Android have to be done in a separate thread), run one of the many functions of the class "InetAddress", which has no wrapper AFAIK. This information is of course useless for most B4A users not familiar with Java, and the InetAddress class, which is THE class to use to check a connection (by sending an echo with ICMP or TCP (port 7) for example), has issues with some servers (it seems that depends mainly on the server configuration). That being said, if the IsReachable() function works with your server, then it does exactly what you need.
BUT my opinion is resumed by this post in another forum:
"If you're pinging as a pre-connection test of availability, just skip it. It's pointless, and the result can change between the ping and the connection. Just try to connect, and if it succeeds, the host is obviously reachable."
Until now, I'm sorry to say that no one convinced me that I and many others were wrong.
 
Last edited:
Upvote 0
Top