Unity3D 网站查找在 Unity3D 4.7.2 中返回 nil [英] Unity3D Website look up returns nil in Unity3D 4.7.2

查看:38
本文介绍了Unity3D 网站查找在 Unity3D 4.7.2 中返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.

        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);
        }

我尝试从统一代码读取游戏的网站链接并读取应用价格.它返回 nil... call 有什么问题?

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

推荐答案

您的代码很好并且工作正常.它没有返回空值. 你认为它返回空值是因为你接收的数据中包含 \n\n 使得 json 在下面的几行开始.要实际查看数据,您必须在控制台"选项卡中向下滚动一点或重新调整下方圆圈中水平线的大小.

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.

虽然在 Unity 中使用 UnityWebRequest 更好,但 HttpWebRequest 也应该可以工作.要回答您的其他问题,下载数据后,请使用 JsonUtility.FromJson 取消-将其序列化为一个对象,然后您就可以访问价格.

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.

这个函数在 C# 中应该是什么样子:

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
            }
        }
    }
}

将 json 反序列化为的对象/类:

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;
}

这篇关于Unity3D 网站查找在 Unity3D 4.7.2 中返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆