using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.Networking;
public class HttpController : MonoBehaviour
{
public InputField zip;
public void BtClick()
{
StartCoroutine(HttpConnect());
}
IEnumerator HttpConnect()
{
string url = "https://zip-cloud.appspot.com/api/search?zipcode=" + zip.text;
UnityWebRequest uwr = UnityWebRequest.Get(url);
yield return uwr.SendWebRequest();
if(uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
Debug.Log(uwr.downloadHandler.text);
}
}
}
Json-parse してみよう。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.Networking;
public class HttpController : MonoBehaviour
{
public InputField zip;
public void BtClick()
{
StartCoroutine(HttpConnect());
}
IEnumerator HttpConnect()
{
string url = "https://zip-cloud.appspot.com/api/search?zipcode=" + zip.text;
UnityWebRequest uwr = UnityWebRequest.Get(url);
yield return uwr.SendWebRequest();
if(uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
string json = uwr.downloadHandler.text;
//Debug.Log(json);
ResultData resultData =
JsonUtility.FromJson<ResultData>(json);
Debug.Log(resultData.results[0].address1);
}
}
}
[Serializable]
class ResultData
{
public List<Address> results;
}
[Serializable]
class Address
{
public string address1;
public string address2;
public string address3;
public string zipcode;
}
BMI-APP
bmiapi
https://joytas.net/php/bmi/api.php?weight=66&height=175
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
public class CanvasController : MonoBehaviour
{
public InputField height;
public InputField weight;
public Text resultText;
public Image resultImage;
public void BtClick()
{
StartCoroutine(HttpConnect());
}
IEnumerator HttpConnect()
{
string url = $"https://joytas.net/php/bmi/api.php?weight={weight.text}&height={height.text}";
UnityWebRequest uwr = UnityWebRequest.Get(url);
yield return uwr.SendWebRequest();
if(uwr.isHttpError || uwr.isNetworkError)
{
Debug.Log(uwr.error);
}
else
{
string json = uwr.downloadHandler.text;
Debug.Log(uwr.downloadHandler.text);
Result result = JsonUtility.FromJson<Result>(json);
Debug.Log(result.bmi);
string imgUrl = result.imgPath;
UnityWebRequest imgUwr =
UnityWebRequestTexture.GetTexture(imgUrl);
yield return imgUwr.SendWebRequest();
if(imgUwr.isHttpError || imgUwr.isNetworkError)
{
Debug.Log(imgUwr.error);
}
else
{
Texture texture =
DownloadHandlerTexture.GetContent(imgUwr);
resultImage.sprite =
Sprite.Create((Texture2D)texture,
new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f));
resultText.text = $"Your BMI is {result.bmi}";
}
}
}
}
[Serializable]
class Result
{
public float bmi;
public string imgPath;
}
scoreapiのurl
url
https://joytas.net/php/scoreapi/GetRanking.php?name=mjpurin&score=32000&sex=0&count=100
コメント