【问题标题】:Unity3D Website look up returns nil in Unity3D 4.7.2Unity3D 网站查找在 Unity3D 4.7.2 中返回 nil
【发布时间】:2018-05-18 09:24:27
【问题描述】:

这是我的代码。

        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...调用有什么问题?

【问题讨论】:

  • 你应该使用WWW

标签: c# iphone xcode unity3d app-store-connect


【解决方案1】:

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

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

这是该函数在 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 反序列化为的对象/类:

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

【讨论】:

  • 感谢您的回答,但在 Unity 4.7.2 中使用 UnityEngine.Networking 时出现错误;这段代码需要 Unity 5+ 还是我想念的任何东西?
  • UnityWebRequest 是 5.3 的功能。更新到该版本以使用它或使用WWW API 甚至您问题中的HttpWebRequest 代码来接收数据。其余的 json 代码应该可以正常工作。请注意,4.7 真的很旧(现在大约 4 岁)。如果可能,您需要至少更新到 Unity 2017。
  • 非常感谢您的建议,我刚刚使用了字符串。包含来自数据的功能并完成了工作。感谢您的精彩提示。
  • 很高兴我能提供帮助
【解决方案2】:

您应该统一使用WWW 或新的UnityWebRequest 类来读取来自互联网的链接。 HttpWebRequest 可以在大多数平台上运行,但在 WebPlayer 或 WebGL 等平台上会失败,因为 javascript 无法访问 IP 套接字。

这是一个改编自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;
            // ...
        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多