u3d上传json到php 并接收php发送的json

2022/6/11 1:20:21

本文主要是介绍u3d上传json到php 并接收php发送的json,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

U3d代码

 class  UserInfo
{
    public string id;
    public string name;

    public UserInfo() { }
    public UserInfo(string id,string name)
    {
        this.id = id;
        this.name = name;
    }
}
public class TestHtml : MonoBehaviour {


    string url = "http://127.0.0.1/test.php";
    byte[] namebyte;
    void Awake()
    {

          //1.用这个构造函数的时候。class UserIfo 不能有pulic UserInfo(){}这个空的构造函数

        //UserInfo userinfo = new UserInfo("wu","wenbo");
        //string nameinfo = JsonMapper.ToJson(userinfo);
        2.用这个构造函数的时候。class UserIfo 需要有pulic UserInfo(){}这个空的构造函数
        UserInfo userinfo = new UserInfo();
        userinfo.id = "wu";
        userinfo.name = "wenbo";
        string nameinfo = JsonMapper.ToJson(userinfo);     
        StartCoroutine(UnityWebRequestPost(url, nameinfo, text => { Debug.Log(text); }));

    }
}
  IEnumerator UnityWebRequestPost(string url, string jsonStr, Action<string> textCallBack)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(jsonStr);
        UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST)
        {
            uploadHandler = new UploadHandlerRaw(bytes),
            downloadHandler = new DownloadHandlerBuffer()
        };
        request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
        yield return request.SendWebRequest();
        if (request.isHttpError || request.isNetworkError)
        {
            Debug.LogError(request.error);
        }
        else
        {
             string result = request.downloadHandler.text;
             Debug.Log(result);
            // string ss = System.Text.Encoding.UTF8.GetString(request.downloadHandler.data, 0, request.downloadHandler.data.Length - 0);
            JsonData objs = JsonMapper.ToObject(request.downloadHandler.text);
            Debug.Log(objs["id"]);
            Debug.Log(objs["name"]);
            Debug.Log(objs.ToJson());             
        }
    }

PHP代码

 

$data = file_get_contents('php://input');
echo $data;

 



这篇关于u3d上传json到php 并接收php发送的json的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程