Unity3D Website look up returns nil in Unity3D 4.7.2

流过昼夜 提交于 2020-01-04 21:30:53

问题


Here is my code.

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://itunes.apple.com/lookup?id=1218822890");
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

        using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using(Stream stream = response.GetResponseStream())
                using(StreamReader reader = new StreamReader(stream))
        {
            string textRead = reader.ReadToEnd();

            Debug.Log("\nData Read = "); Debug.Log(textRead);
        }

I tried to read game's website link from unity code and read app price. It returns nil...what's wrong with call ?


回答1:


Your code is fine and is working correctly. It is not returning null. You think it is returning null because the data you are receiving contains \n\n in it making have the json to start in on several lines below. To actually see the data, you have to scroll down a bit in the Console tab or re-size the horizontal line in the circle below.

Although, it is better to use UnityWebRequest in Unity but HttpWebRequest should also work. To answer your other question, once you download the data, use JsonUtility.FromJson to de-serialize it into an Object then you can access the price.

Here is what that function should look like in C#:

void Start()
{
    StartCoroutine(CheckForPaidApp("http://itunes.apple.com/lookup?id=1218822890")); ;
}


IEnumerator CheckForPaidApp(string uri)
{
    UnityWebRequest uwr = UnityWebRequest.Get(uri);
    yield return uwr.SendWebRequest();

    if (uwr.isHttpError || uwr.isNetworkError)
    {
        Debug.Log("Error While Sending: " + uwr.error);
    }
    else
    {
        string data = uwr.downloadHandler.text;
        Debug.Log("Received: " + uwr.downloadHandler.text);

        //Serialize to Json
        RootObject jsonObj = JsonUtility.FromJson<RootObject>(data);
        List<Result> resultObj = jsonObj.results;

        //Loop over the result and show the price information
        for (int i = 0; i < resultObj.Count; i++)
        {
            double price = resultObj[i].price;
            Debug.Log("Price = \n" + price);

            if (price > 0.0f)
            {
                Debug.Log("Its Paid App\n");
            }
            else
            {
                // show ads here
            }
        }
    }
}

The Objects/classes to de-serialize json the into:

[Serializable]
public class Result
{
    public List<string> screenshotUrls;
    public List<string> ipadScreenshotUrls;
    public List<object> appletvScreenshotUrls;
    public string artworkUrl512;
    public string artworkUrl60;
    public string artworkUrl100;
    public string artistViewUrl;
    public List<string> supportedDevices;
    public string kind;
    public List<string> features;
    public bool isGameCenterEnabled;
    public List<object> advisories;
    public string fileSizeBytes;
    public List<string> languageCodesISO2A;
    public string trackContentRating;
    public string trackViewUrl;
    public string contentAdvisoryRating;
    public string trackCensoredName;
    public List<string> genreIds;
    public int trackId;
    public string trackName;
    public string primaryGenreName;
    public int primaryGenreId;
    public string currency;
    public string wrapperType;
    public string version;
    public int artistId;
    public string artistName;
    public List<string> genres;
    public double price;
    public string description;
    public string bundleId;
    public string sellerName;
    public bool isVppDeviceBasedLicensingEnabled;
    public DateTime releaseDate;
    public DateTime currentVersionReleaseDate;
    public string minimumOsVersion;
    public string formattedPrice;
}

[Serializable]
public class RootObject
{
    public int resultCount;
    public List<Result> results;
}



回答2:


You should use WWW or the new UnityWebRequest classes in unity to read links from internet. HttpWebRequest will works on most platforms but will fail on some like WebPlayer or WebGL as javascript can't access IP Sockets.

Here is an example adapted from the Documentation

public class ExampleClass : MonoBehaviour
{
    public string url = "http://itunes.apple.com/lookup?id=1218822890";
    IEnumerator Start()
    {
        using (WWW www = new WWW(url))
        {
            yield return www;
            string textRead = www.text;
            // ...
        }
    }
}


来源:https://stackoverflow.com/questions/50408020/unity3d-website-look-up-returns-nil-in-unity3d-4-7-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!