Unity发送Post和Get请求

 以发送Get 和 Post请求 返回json字符串为例:

发送Post请求:

    protected IEnumerator SendPost(string url, WWWForm wForm = null)
    {
        if (!string.IsNullOrEmpty(url))
        {
            WWW result = new WWW(url, wForm);

            yield return result;

            if (result.error != null)
            {
                Debug.Log("访问失败:" + result.error);

            }
            else
            {
                if (string.IsNullOrEmpty(result.text))
                {
                    Debug.LogError("返回值为空");
                }
                else
                {
                    Debug.Log(result.text);
                }
            }
        }
        else
        {
            Debug.LogError("URL不能为空");
        }

    }

发送Get请求:

    protected IEnumerator SendGet(string url)
    {
        if (!string.IsNullOrEmpty(url))
        {
            WWW result = new WWW(url);

            yield return result;

            if (result.error != null)
            {
                Debug.Log("访问失败:" + result.error);
            }
            else
            {
                if (string.IsNullOrEmpty(result.text))
                {
                    Debug.LogError("返回值为空");
                }
                else
                {
                    Debug.Log(result.text);

                }

            }
        }
        else
        {
            Debug.LogError("URL不能为空");
        }

    }

返回json字符串处理:

json格式:

        {
            "name": "张三",
        }

 

unity自带的解析方法部分处理不理想,所以使用第三方库LitJson。

    public void JsonToClass(string input)
    {
        if (!string.IsNullOrEmpty(input))
        {
            JsonData result = JsonMapper.ToObject(input);

            if (result.Keys.Contains("name"))
            {
                //使用了一次result["name"].ToString()转字符串  因为有时候如果不转字符串 会出现奇怪的问题
                string name = Convert.ToString(result["name"].ToString());
            }
            else
            {
                Debug.LogError("name 不存在");
            }
        }
    }

如果返回的是列表 比如:

{
    "player": [
        {
            "id": "1",
            "name": "张三",
        },
        {
            "id": "2",
            "name": "李四",
        },
    ]
}

解析方法稍作改变:

    public void JsonToClass(string input)
    {
        if (!string.IsNullOrEmpty(input))
        {
            JsonData result = JsonMapper.ToObject(input);

            if (result.Keys.Contains("player"))
            {
                JsonData jsonData = result["player"];

                for (int i = 0; i < jsonData.Count; i++)
                {

                    if (jsonData[i].Keys.Contains("id"))
                    {
                        int id = Convert.ToInt32(jsonData[i]["id"].ToString());
                    }
                    else
                    {
                        Debug.LogError("第"+i+"个数据 id不存在");
                    }

                    if (jsonData[i].Keys.Contains("name"))
                    {
                        int id = Convert.ToString(jsonData[i]["name"].ToString());
                    }
                    else
                    {
                        Debug.LogError("第"+i+"个数据 name不存在");
                    }
        }
    }

 


版权声明:本文为qq_26723085原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。