Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.

Checking the internet connection

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-02-2009, 01:43 AM
Senior Member
 
Join Date: Sep 2008
Posts: 161
Default Checking the internet connection

Based on another thread, here is a simple way to check if the device has a valid internet connection and show an appropriate error message.

You will need the HTTP library. Response is a Webresponse object and request is a webrequest object.

Let's say you want to download a file when your application starts:

Code:
Sub App_Start
 DownloadStuff
End Sub
If there is no response from the server, it will raise an error in your sub. When an error is raised, the execution of the sub stops. To avoid this, you must check if the connection is available by calling another thread.

Code:
Sub App_Start
 
If CheckConnection=True Then
  DownloadStuff
 
End If
End Sub

Sub CheckConnection(URL)
ErrorLabel(offline)
  Request.New1(URL)
  Response.New1
  Response.Value = Request.GetResponse
  Response.Close
   
Return True
offline:
   
Return False
End Sub
This way, when CheckConnection is called, it returns the connection status. However nothing will happen if there is no connection. No error message, nothing! Let's fix this by adding a new sub: NoConnection.
Code:
Sub App_Start
 
If CheckConnection=True Then
  DownloadStuff
 
End If
End Sub

Sub CheckConnection(URL)
ErrorLabel(offline)
  Request.New1(URL)
  Response.New1
  Response.Value = Request.GetResponse
  Response.Close
   
Return True
offline:
   
Return False
End Sub

Sub NoConnection
 
MsgBox("Unable to download application")
End Sub
Now you get a nice error message if there is no connection. However, to avoid confusing the user if the problem is caused by an invalid URL or server downtime, it is good practice to distinguish the internet connection problems from the server problems.

Code:
Sub NoConnection
 
If CheckConnection("http://google.com")=True Then
  
MsgBox("Unable to find a valid data connection.")
 
Else
  
MsgBox("Unable to download file. The server might be experiencing difficulties.")
 
End If
End Sub
Voilą!
__________________

Last edited by N1c0_ds : 01-02-2009 at 01:47 AM.
Reply With Quote
  #2 (permalink)  
Old 01-02-2009, 02:57 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,726
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Thank you for sharing this important tip
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking if registry key exists? N1c0_ds Questions (Windows Mobile) 3 10-21-2008 05:30 AM
GPRS / Dialup - Internet Connection TWELVE Questions (Windows Mobile) 9 06-24-2008 06:35 AM
CASE - checking for a range ? TWELVE Questions (Windows Mobile) 1 06-21-2008 08:00 PM
Checking available fonts? MitchDabo Questions (Windows Mobile) 5 02-10-2008 10:43 PM
checking textbox string derez Questions (Windows Mobile) 13 10-25-2007 11:58 AM


All times are GMT. The time now is 10:19 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0