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

查看:175
本文介绍了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 ...呼叫有什么问题?

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

推荐答案

您的代码可以正常运行.它不会返回null.您认为它正在返回null,因为所接收的数据中包含\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天全站免登陆