Пример #1
0
        /**
         *   //注册请求 POST
         *  Dictionary<string,string> dic = new Dictionary<string, string> ();
         *  dic.Add("Action","1");
         *  dic.Add("usrname","xys");
         *  dic.Add("psw","123456");
         *
         *  StartCoroutine(POST("http://192.168.1.12/login.php",dic));
         * */
        //POST请求
        IEnumerator POST <T>(string url, Action <object, object> callBack, Dictionary <string, object> param)
        {
            WWWForm form = new WWWForm();

            if (param != null)
            {
                foreach (KeyValuePair <string, object> post_arg in param)
                {
                    if (post_arg.Value != null)
                    {
                        form.AddField(post_arg.Key, post_arg.Value.ToString());
                    }
                }
            }



            //第一种方法
            //这是get请求的头参数
            Hashtable headers = null;// form.headers;

            //headers.Remove ("Content-Type");
            //headers.Add("Content-Type", "application/json");
            //headers.Add("Content-Type", "application/x-www-form-urlencoded");

            if (param.ContainsKey("token"))
            {
                headers.Add("Authorization", param["token"]);
            }

            WWW www = new WWW(url, form.data, headers);

            //第二种方法
            //WWW www = new WWW(url, form);

            yield return(www);

            if (www.isDone)
            {
                string data = "";
                if (www.error != null)
                {
                    data = "{'result':-100,'msg':'" + www.error + "'}";
                    //PopMessageManager.show(data);
                }
                else
                {
                    data = www.text;
                    //PopMessageManager.show("ok");
                }

                if (callBack != null)
                {
                    Echo.Log("callBack:" + data);

                    T t = JsonMapper.ToObject <T> (data);
                    Filter(t);
                    callBack(t, data);
                }
            }
        }