Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Check Internet Connection In Android

Discussion in 'Scripting' started by Meqara, Jun 14, 2018.

  1. Meqara

    Meqara

    Joined:
    Feb 17, 2018
    Posts:
    14
    Hello,
    I want to check the internet connection in Android, so I use this code and it works but the checking is slow and while checking the app does not respond until it finishes checking.
    So is there any ideas to solve this, or any other scripts for checking the connection.

    Code (CSharp):
    1. public string GetHtmlFromUri(string resource){
    2.         string html = string.Empty;
    3.         HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource);
    4.         try
    5.         {
    6.             using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
    7.             {
    8.                 bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200;
    9.                 if (isSuccess)
    10.                 {
    11.  
    12.                     using (TextReader reader = new StreamReader(resp.GetResponseStream()))
    13.                     {
    14.                         //We are limiting the array to 80 so we don't have
    15.                         //to parse the entire html document feel free to
    16.                         //adjust (probably stay under 300)
    17.                         char[] cs = new char[80];
    18.                         reader.Read(cs, 0, cs.Length);
    19.                         foreach(char ch in cs)
    20.                         {
    21.                             html +=ch;
    22.                         }
    23.                     }
    24.                 }
    25.             }
    26.         }
    27.         catch
    28.         {
    29.             return "";
    30.         }
    31.         return html;
    32.     }
    33.     void Start()
    34.     {
    35.         string HtmlText = GetHtmlFromUri("http://google.com");
    36.         if(HtmlText == "")
    37.         {
    38.             //No connection
    39.             Debug.Log("Connection Not Found");
    40.         }
    41.         else if(!HtmlText.Contains("schema.org/WebPage"))
    42.         {
    43.             //Redirecting since the beginning of googles html contains that
    44.             //phrase and it was not found
    45.         }
    46.         else
    47.         {
    48.             //success
    49.             Debug.Log("Connection Found");
    50.         }
    51.     }    
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,976
    You should just be able to use UnityWebRequest to do a similar thing. Do it in a coroutine, and that way you can keep some loading indicator running or something. You don't have to read in the contents either, just know that you downloaded something and didn't get an error.

    We do something similar where we download a 1 pixel image from our server.
     
  3. Meqara

    Meqara

    Joined:
    Feb 17, 2018
    Posts:
    14
    Thank you very much, it works.
    This is the new code:
    **Code is EDITED** (I add this line of code: && www.progress <= 0.5 at line nb. 10)
    Code (CSharp):
    1. private IEnumerator CheckConnection(string url)
    2.     {
    3.         isRunning= true;
    4.         WWW www = new WWW(url);
    5.         float elapsedTime = 0.0f;
    6.  
    7.         while (!www.isDone)
    8.         {
    9.             elapsedTime += Time.deltaTime;
    10.             if (elapsedTime >= 10.0f && www.progress <= 0.5) break;
    11.                 yield return null;
    12.         }
    13.  
    14.         if (!www.isDone || !string.IsNullOrEmpty(www.error))
    15.         {
    16.             Debug.LogError("Load Failed");
    17.             NoConnectionPanel.SetActive(true);
    18.             isRunning= false;
    19.             yield break;
    20.         }
    21.  
    22.         NoConnectionPanel.SetActive(false);
    23.         isRunning= false;
    24.     }
    25.  
    26. private Update()
    27.     {
    28.         if (!isRunning)
    29.         {
    30.              StartCoroutine(CheckConnection("google.com"));
    31.          }
    32.     }
     
    Last edited: Jun 14, 2018