Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. 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 at 8:36 PM.

  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,672
    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 at 11:30 PM