Checking connection is very easy. You can use a Web component and try connecting to a website. Google.com is a very good candidate for that since it’s very unusual for google server to be down. Below is a snippet where I have a button and when you click the button, it tries to connect to google.com. You can download the project CheckConnection and try changing the google url to something that doesn’t exist and see the result. Say if you set the value of the variable googleUrl to http://www.google.com/1234/5678, you will have a 404 error. If the responseCode sent from the server is 200, that means you have connection.
For a list of response codes and what they mean, see HTTP status codes.
Here’s the project source – CheckConnection
I’m looking for a way to such action, whether it is possible to:
a) when you start Screen1, the application checked if an active internet connection, if so OK, run Screen2
b) if there is no Internet connection, run Screen3
I thought your code to check for my needs. Unfortunately does not work as it should, there is an error code 1101 for the server. I can customize it to my needs.
Please help, thanks Tom
Did you try this on a device? You should test connection related apps on your device. You can try first the sample CheckConnection provided above.
Hello. If the web server does not work, how to check internet connectivity?
I thank you for your help;) objective achieved. My code is able to:
First checks the Internet connection (Screen1)
2 if there is an active Internet connection, launches Screen 3
3 if there is not an active Internet connection, launches Screen 2
I will try to fine-tune the code to my needs.
I’m looking for a solution like yours. U have to send me the maid to file aia have a study .
I have a question, how to build the code in the app inventor not inventor2? Is it possible, I can not odnaleść ingredients to inventor2?
You can see my old tutorial. Just scroll down-
Thank you for your help again;). After a large reconstruction of the code, I got what I was looking for. You are great
I am glad to hear you got it working.
Can i call ajax request xmlhttp request from app inventor
You should look at http://imagnity.com/tutorials/app-inventor/web-component-json-response-on-app-inventor/#comment-291.
I have a question, how can i read the unique ID of an NFC tag on android, please (App inventor 2)
Currently it’s limited- http://ai2.appinventor.mit.edu/reference/components/sensors.html#NearField
Here’s a simple tutorial-
Thanks for your answer, its right; based on the information that you mentioned, I have developed an application that can read and write the labels, and connect to internet. However, I do need to be able to obtain the ID on the NFC card. I know that the getID on java method is used, but I don’t know how to implement it on app inventor 2. Any suggestions? Can I use PHP on app inventor 2 to get it?
Currently not possible. If you are interested, you can work with source and add whatever you need. A lot of folks have their own modified versions. Start here- http://appinventor.mit.edu/appinventor-sources/
I have a question,how can I post a username and password to a website..?
Is it possible in appinventor..?
You can build the url with necessary data. Do a search in google. This has been explained in many sites.
Hi, have you tried this app on Lollipop ? I’ve tried it on a Nexux 5 with lollipop and it doesn’t seems to work. It shows that he doesn’t have internet connection, even when the device is connected. Any ideea why ? On other Android versions the app works perfect.
The ResponseCode from google.com when you are using Web.Get on Lollipop is 302, not 200, and so, the answer of the App is that you have no internet connection, even if you are connected. Do you know how can i check the Android version in Appinventor, so i can modify the ‘IF condition’ acoording to it ?
Thank you very much for your answer.
302 is valid with redirection. Modify the ‘if’ statement to “if responseCode=200 or responseCode=302”, it’s good.
Ok. I’ll do that. I thought that 302 is a real error, not an ‘OK’answer. I was already looking for another way to check the internet connection, no way I was thinking about implementing this very simple solution (so stupid of me:) ). Thank you very much, again. By the way, there is some kind of method in appinventor for sending a ‘ping’ message to an IP and check if you get the reply ?
Thank you !
When you try reaching http://www.google.com, it may redirects to https://www.google.com/?gfe_rd=cr&ei=blahblahblah. That is why you get 302. It’s not an error. http://google.com is not the same as http://www.google.com.
To ping a url, you should look at ‘PostTextWithEncoding’ of Web component. You can construct the encoding with ‘User-Agent’ and ‘connection’ being ‘close’. Maybe google can help more.
http://www.google.com dont work anymore
you have to change it to https://www.google.com
It does work. It sends 302 for redirect and 200 for final entry. Since we only catch the first response, we get 302.
This doesn’t work anymore. I had it working just fine on an old app, needed it in a new one, and it fails. I tried the old app, doesn’t work either. I get an Error 1101, unable to get response. Can you help me with a sollution
You need to upgrade your companion first.
Thanks Sajal for this tutorial!
It’s working on Android 5.1 adding “if responseCode=200 or responseCode=302″.
Have you done in App Inventor an Internet speed meter?
There are too many variables involved to do that. Do a search online and you will see what I mean. An example is – while you are making the speed test, say device is scheduled to fetch mails in the same time. Your speed test will be affected.
First thanks For Sharning and i useing your source code “CheckConnection” i want this app ping evrey 1 min ,What should I do?
Have a Clock and set its interval to 2000 ms. Then every time Clock fires, ping.
First of all, thank you for sharing
I want change “CheckConnection” it, for example, every 1 minute ping http://www.google.com and, if conection down show a msg
what should I do?
See my response above.
Thank you very much for your response
and how to i can get ,(for example), 3 website from textbox and app ping all web sit in order?
Have a list of URLs o ping to and then iterate through the list and ping. Check out the list tutorial. http://imagnity.com/tutorials/app-inventor/list-blocks-on-app-inventor/
Thank you very much for fast response
how to run app in background?
Short answer, no you cannot. Something like this you can look at: http://appinventor.mit.edu/explore/content/alertme.html
Oh,That’s no way?
I have some questions that I did not find in your post:
What is a Code for Error connect to the Internet?
How do identify that a site is busy (responseCode)?
What is a responseCode for Down URLs?
this Is code are reliable?
I have a texbox for Enter url from user,When i,press Button home on the my Phone to visible screen,and again restore it,textbox is clear!!do you have solution
hello, i want to build voltmeter by using apps and internet. Do you have any suggestion to build it if i want to read data of voltage from circuit that i created on arduino. means that data from arduino will send to android phone using mit apps inventor 2 using internet.
Read the link below. It also has code sample. http://appinventor.mit.edu/explore/stories/app-inventor-app-sends-arduino-sensor-data-internet.html
Hi Sajal. Is there a way to diactivates the Error 1001 Message? Or it always will be displayed when no internet connection was detected?
It’s upto you if you want to display any message or not. Remove call notifier block which displays “Connection Error!” message so that no message is shown when there’s a 1101 error.
I mean this error: https://drive.google.com/file/d/0BxH4nKsGhSyceHNLeGlDSzg4eGs/view?usp=sharing
wich is called by the web component itself.
Sry, now I get it. Thx.
Hello Sajal, thanks for the tutorial and the example provided.
Im actually using Thunkable, spin off AI2, and i have Screen1 Webviewer component.
I would like that if there is no connection the app will go to Screen2 where i had put two textbox.
Please can you help me how i can achieve that?
Should be very easy. Open Screen2 instead of showing a message (‘Connection Successful!’) like I did above. To open, use ‘open another screen’ block from Control drawer.
Uhm seems nothing happening Sajal. If there is no connection the app does nothing, just show the default webview error message.
I just added the web1 component in the designer and the 2 blocks to my Screen1
I misunderstood. I thought if connection is successful, you want to launch another screen. You want other way around. In web1.GotText, have a if statement where it checks if the response code is NOT 200. If not 200, open your other screen. Do the same in errornumber=1101.
Yes Sajal i want in the other way.
But the problem is that the Web1 seems is not triggered when the app open.
I added When Screen1.OnInitalize do call Web1.get but with no success, where im wrong?
If you want i can provide the .aia app file (but don’t know if it’s compatible with AI2 as i created in Thunkable)
@Simone, send me your .aia. Hopefully that will open in ai2.
Ok Sajal and thanks.
Here it is: https://drive.google.com/file/d/0B7gT5rRv5MLBdG51TFI2bk40NkU/view?usp=sharing
I done a test and imported it in AI2 and it works! It just tell you a warning message that it’s saved with a newer version and some functions could be not compatible, but seems all ok.
You didn’t set the Url of Web1. Try setting it to google.com as I did above in the tutorial. Use an actual device to test, not an emulator. Everything should work unless something is wrong with thunkable thing that you used which I have no idea about. Contact them.
Ok i will try Sajal, but something isn’t clear to me, why i have to set the Url of Web1 if i already set an Url in the WebViewer component?
I just want that if there is no connection the app will go to Screen2, if there is connection it will display the content of the WebViewer component.
Web viewer has nothing to do with web. Viewer just views a page like a browser while web is much more. It can post a request, fetch a server response, etc. web is used to get a response from any server. In my tutorial, I chose google.
I solved Sajal! Thanks for all the support.
Thank you for this post.
I am trying to open a URL that does not exist. For some reason screen1.erroroccured event is not triggered. Only when I disable the DATA on the phone it is triggered. Do you know why and how to resolve?
See first what response code you get. I provided a link in this tutorial to see what those response codes mean. You should receive 404 (not found). By the way, having no connection is not an error. It’s a state.
In app inventor 2 is possible to check if the user using WiFI or 4G?
I am afraid you cannot. Look for a site/api that checks if the mobile network is 3g/4g/etc and use that.
I want make an app that itself starts as user turn on data connection like whats app , hike .
Please provide solution and also want to know that how it can be run inside without user permission
I want to.make app which should start as user turn on data connection.
I am working on an project where i need to pass a hostname in the url instead of the IP Address. Is it possible in App Inventor.
Is there a way of getting the IP of the HOST using getByName() or some other java method.
Any pointers in this direction will help.
i am unable to use check connection
Can you please elaborate?
sir can you help me?
i’m trying to create a simple registration (part of my app),
i’m sure that the db credentials is correct and when running the app doing registration, it returns “SUCCESS” but the data isn’t save on my database what i’m doing wrong?
php script: http://imgur.com/SkkuU4T
im really don’t know what to do know and can’t find solution on the internet
i hope you can give me some tips sir
thank you very much
You are not setting the headers. See here: http://puravidaapps.com/mysql.php
Can you help me to send sms using app inventor while compiling it gives error only one screen i checked it gives error ie.ymail.
i googled but didn’t get result.
Can you please post the error screenshot here?
Hi!. I’ve tried this and works superb!, thanks for that ^^. But my application needs to work with prepaid navigation, so it’s very important to restrict and reduce the data used on this operation. Is there any way to make this a bit more cheaper?. Thanks for all your work!!.
thanks sir for your important subject
i want ask you abaut send firebase cloud msg (notification )
i set every thing in google console and firebase console
what can i do in my project to receive my msg ?
the else part will never work…. because there is simply no connection to get the text file.
Hello Mr. Dutta,
I am new to android apps. I am trying to make an app for my office purpose in which files (jpg, pdf) stored in a ftp server can be viewed in app. I can make a connection to office ftp server through VPN. I request u to help me in providing some useful tips about VPN to my office server.
Hi Mr. Dutta,
I am using a program on App Inventor 2 to communicate with ESP8266 wifi module. As a test, initially I wrote so that the smartphone will send messages type=1=, type=2= etc (= is the delimiter used by program on ESP8266. When the ESP reads the message, it will send a response “Response=1”, “Response=2″ etc. When I was testing in one of the iterations, there was a bug in Type=4 iteration and the program stopped. In Type 4, I am getting the current day (as 1 for Sunday, 2 for Monday etc.) and time (11:51;42 AM etc) from the clock function. Now, even though the program is fixed, whenever I start the app, I get the following message on the smartphone app ” Unable to get a response with the specified URL:http://?type=4=3=1:51:13 PM= where the current day and time represent the day and time when the app is currently running. How do I clear this? I tried looking at the files in the smartphone but could not zero in on the file to remove.
How to monitor the internet connectivity. For instance, suppose it loses internet connectivity and again gains the connectivity we want to change the pages based on connectivity spontaneously.
Use a timer with 5000 ms (5 seconds) to check connection.
I’m getting the 1101 error whenever I try running any apps that have the Web component. Any tips on how I can eliminate that error? I have tried the apps using my home wifi, school wifi and with mobile data but still no luck. Please help!
I have a cell phone with “2 chips”. I need to turn data usage on and off whenever I want. Is there any block / event in appinventor2 with the function of picking the chip and turning on and off data usage?
I want to check wifi esp8266 connection in app invertor.How does it?
Excellent tutorial Sajal.
When my app first check internet connection, it’s always false.
I want to connect to an esp8266 that is in softap mode, running an internal webserver (without internet connection), when I run the app (written in appybuilder and using the taifu wifi extension) it connects, it tells me that it does not have access to the internet (in the smartphone) and return to the pre-established connection with the internet, can you help me please, thanks, apologies for my english
thanks a lot!
good work Your CheckConnection app!
My mobile sometimes loses the 3G internet connection and manually I stop and restart data connection for 2/3 times.
Your app check correctly the status, but is possible to diseble and enable the mobile data connection via code?
I am using web server on ESP-01 (ESP8266 Module) to communicate with with mobile phone. I send various commands from mobile phone to ESP-01 module and interfaced ESP-01 further with another MCU. As I send commands using web server I am unable to communicate with ESP-01 using mobile phone if mobile data of phone is turned on. To successfully send the commands mobile data is needed to be turned off. Is there any way using which I can turn on or off mobile data as user open app or the web server can be controlled with turning off mobile data.
I am stuck on this from last few weeks. Please help me to find some solution.
Thank You in advance 🙂