Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Created December 28, 2013 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masayuki5160/8159508 to your computer and use it in GitHub Desktop.
Save masayuki5160/8159508 to your computer and use it in GitHub Desktop.
UnityでGetとPOST通信のテスト
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