简体   繁体   中英

How to Find the Country Name From Untiy

Hi All I am trying to find my country name from unity. The Below code works from System. It did not works on iOS. So whats i have to do

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.Networking;
  using UnityEngine.UI;

    public class Location2 : MonoBehaviour
   {
  // Start is called before the first frame update
   public byte[] results;
   public string str2;
   public string[] authorsList;
   public Text text;


void Start()
{
    StartCoroutine(GetText());
    Invoke("F", 3f);
}

public  IEnumerator GetText()
{
    Debug.Log("Get Text");
    UnityWebRequest www = UnityWebRequest.Get("https://extreme-ip-lookup.com/json/");
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
    }
    else
    {
        // Show results as text
        Debug.Log(www.downloadHandler.text);
        str2 = www.downloadHandler.text.ToString();
        authorsList = str2.Split(' ');
       
        // Or retrieve results as binary data
    
    }
}

public void F()
{
    text.text = authorsList[25];
// It displays country name 
}


}

I dont want use any location based finding. Why the above code did not works on iOS.

Why do you think you can simply assume that your download always is finished after 3 seconds?

Instead of blind trust and invoke the F method after hard coded 3 seconds rather wait until the download is actually done:

IEnumerator Start()
{
    using(var www = UnityWebRequest.Get("https://extreme-ip-lookup.com/json/"))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            text.text = www.error;
            yield break;
        }

        // Show results as text
        str2 = www.downloadHandler.text;
    }

    authorsList = str2.Split(' ');
       
    F();
}

public void F()
{
    text.text = authorsList.Length >= 26 ? authorsList[25] : "error: no 26 elements";
}

Then besides that what you get from that API is actually a JSON. So what you rather should do is have a class

[Serializable]
public class Root   
{
    public string businessName; 
    public string businessWebsite; 
    public string city; 
    public string continent; 
    public string country; 
    public string countryCode; 
    public string ipName; 
    public string ipType; 
    public string isp; 
    public string lat; 
    public string lon; 
    public string org; 
    public string query; 
    public string region; 
    public string status;  
}

And then simply use

IEnumerator Start()
{
    using(var www = UnityWebRequest.Get("https://extreme-ip-lookup.com/json/"))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            text.text = www.error;
            yield break;
        }

        var root = JsonUtility.FromJson<Root>(www.downloadHandler.text);

        text.text = root.country;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM