When unity3d is communicating with. net over http, the most common thing is to submit the form data request, but the server will return a lump of json data, which requires us to process the json data in unit. Generally, the most common way to process the json data in unit is litjson (it is the class library that processes the SON database under the. net platform). Below I post the source code, only for learning reference!
For the installation and use of LitJSON, please refer to: http://www.360doc.com/content/13/0117/11/10941785_260686840.shtml
Or refer to: http://blog.csdn.net/dingxiaowei2013/article/details/17115665
Put LitJson.dll in the plugins file in the assets directory. If there is no plugins file, create one manually
Client:
- using UnityEngine;
- using System.Collections;
- using LitJson;
- public class GetPhotoList : MonoBehaviour {
- // Use this for initialization
- void Start () {
- StartCoroutine(GetPhotos());
- }
- // Update is called once per frame
- IEnumerator GetPhotos(){
- WWWForm form = new WWWForm();
- form.AddField("id","123");
- WWW w = new WWW("http://localhost:36944/GetPhotoList.ashx",form);
- while (!w.isDone)
- if (w.error != null)
- Debug.Log(w.text);
- JsonData jd = JsonMapper.ToObject(w.text);
- for (int i = 0; i < jd.Count; i++)
- {
- Debug.Log("id=" + jd[i]["id"]);
- Debug.Log("name=" + jd[i]["name"]);
- }
- }
- }
Server:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Runtime.Serialization.Json;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.IO;
- namespace UpdatePhoto
- {
- /// <summary>
- ///Summary description for GetPhotoList
- /// </summary>
- public class GetPhotoList : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- string id = context.Request.Form["id"];
- string path = context.Request.PhysicalApplicationPath;
- //context.Response.Write("Hello World");
- List<Photo> photos = GetPhotos(id,path);
- DataContractJsonSerializer djson = new DataContractJsonSerializer(photos.GetType());
- djson.WriteObject(context.Response.OutputStream, photos);
- }
- public List<Photo> GetPhotos(string id,string path)
- {
- //Get directory
- string localPath = path+id + "\\;
11 May 2020, 12:53 | Views: 9825
Add new comment
0 comments