- Home /
Need Help in Networking i.e WWW
Hi I'm trying to send the json data to server and my line of code is :
JSONObject j = new JSONObject(JSONObject.Type.OBJECT);
for(int i=0;i<Contacts.ContactsList.Count;i++)
{
if(Contacts.ContactsList[i].Phones.Count>0)
{
JSONObject contactList = new JSONObject(JSONObject.Type.ARRAY);
JSONObject phoneNumber = new JSONObject(JSONObject.Type.ARRAY);
j.AddField("Contacts",contactList);
contactList.AddField("Name",Contacts.ContactsList[i].FirstName+""+Contacts.ContactsList[i].LastName);
contactList.AddField("PhoneNumber",phoneNumber);
for(int k=0;k< Contacts.ContactsList[k].Phones.Count;k++)
{
phoneNumber.Add(Contacts.ContactsList[i].Phones[k]);
}
string jsonData = j.Print();
Dictionary<string, string> header = new Dictionary<string,string>();
header.Add("Content-Type","application/json");
byte[] body = System.Text.Encoding.UTF8.GetBytes(jsonData);
WWW www = new WWW(URL,body,header);
StartCoroutine(PostDataEnumerator(www));
}
}
The problem is I don't know what it do . Can anyone help me with it to know what does it do and in which form of data the server recieve. thanks
Answer by sys12 · Sep 23, 2015 at 12:08 PM
WWW www = new WWW(URL,body,header);
This will submit your Json data in bytes to the URL given. If you don't know about this, you can use WWWForm instead. That might be much more straight forward to you. http://docs.unity3d.com/ScriptReference/WWWForm.html
What it returns is the data get from your server. And the return type depends on your operation. For example, www.text
returns the string form of the data and www.bytes
returns the byte form of your result. And WWW www = new WWW(URL,body,header);
should be stated inside coroutine body if you want it to provide concurrency. http://docs.unity3d.com/ScriptReference/WWW.html