Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. 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.     }    
     
    cankirici34 likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    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.
     
    cankirici34 likes this.
  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
    cankirici34 and richGiraldo like this.
  4. cankirici34

    cankirici34

    Joined:
    Mar 27, 2020
    Posts:
    14
    Still works like a charm!