Created
December 28, 2013 13:28
-
-
Save masayuki5160/8159508 to your computer and use it in GitHub Desktop.
UnityでGetとPOST通信のテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using MiniJSON; | |
public class TestHttp : MonoBehaviour { | |
public GameObject sphere; | |
// Use this for initialization | |
void Start () { | |
// StartCoroutine (Download()); | |
StartCoroutine (Post()); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
IEnumerator Download() | |
{ | |
WWW www = new WWW("http://hoge/two.json?num=3"); | |
// wait until JSON Contents will come | |
yield return www; | |
if(www.error != null){ | |
Debug.Log("Error!"); | |
}else{ | |
Debug.Log("Success"); | |
//parse the JSON | |
var jsonData = MiniJSON.Json.Deserialize(www.text) as Dictionary<string,object>; | |
//string name = (string)jsonData["multiply"]; | |
//long num = (long)jsonData["num"]; | |
long num = (long)jsonData["multiply"]; | |
int tmp = (int)num; | |
for(int i = 0; i < tmp; i++){ | |
Instantiate(this.sphere,this.transform.position,Quaternion.identity); | |
} | |
} | |
} | |
IEnumerator Post() | |
{ | |
WWWForm form = new WWWForm(); | |
form.AddField ("num", "4"); | |
WWW www = new WWW ("http://hoge/two.json", form); | |
// wait until JSON Contents will come | |
yield return www; | |
if(www.error != null){ | |
Debug.Log("Error!"); | |
}else{ | |
Debug.Log("Success"); | |
//parse the JSON | |
var jsonData = MiniJSON.Json.Deserialize(www.text) as Dictionary<string,object>; | |
//string name = (string)jsonData["multiply"]; | |
//long num = (long)jsonData["num"]; | |
long num = (long)jsonData["multiply"]; | |
int tmp = (int)num; | |
for(int i = 0; i < tmp; i++){ | |
Instantiate(this.sphere,this.transform.position,Quaternion.identity); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment