- Home /
Question by
stargamingentertainment · Nov 30, 2016 at 01:22 PM ·
c#unity 5dictionary
How to call the post function of the class Unity post get check www requests with success callback and error callbacks,
Hi Everyone this is my first time of asking a question of the whole 4 years of programing
im just wondering if any one could help point me in the right direction please?? or even better reply with the solusion please.
ive found this code on stackexchange.com link (http://codereview.stackexchange.com/questions/148550/unity-post-get-check-www-requests-with-success-callback-and-error-callbacks).
I cant seem to figure out how to call the post function passing through parameters for the dictionary.
this is partly how u call the post function
MHSOnline.Post("register.php", "what do i have to put in here to add to the ", success => {
// Success
}, failure => {
// Failure
});
I've searched all other the internet but no luck, would someone please be able to help
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace MugHeadStudios
{
/// <summary>
/// MonoBehaviourEmpty is a dummy class added to run coroutines on temporary GameObjects
/// </summary>
public class MonoBehaviourEmpty : MonoBehaviour {}
/// <summary>
/// MHS online.
/// </summary>
public class MHSOnline
{
// PROPERTIES
static public string Domain;
static public string DirectoryPath;
static public string PingFile;
public delegate void WWWCallBack(string RequestedString = "");
public delegate void WWWErrorCallBack(string RequestedString = "");
// PUBLIC
/// <summary>
/// Post request to the specified URL with Data, optional CallBack and ErrorCallBack.
/// </summary>
/// <param name="File">File.</param>
/// <param name="Data">Data.</param>
/// <param name="CallBack">Call back.</param>
/// <param name="ErrorCallBack">Error call back.</param>
static public void Post(string URL, Dictionary<string, string> Data, WWWCallBack CallBack = null, WWWErrorCallBack ErrorCallBack = null)
{
MHSOnline.NetworkAvailable(success => {
if(CallBack != null)
{
GameObject newGameObject = new GameObject("Co-routine (POST)");
newGameObject.AddComponent<MonoBehaviourEmpty>().StartCoroutine(MHSOnline.IRequest(URL, Data, CallBack, newGameObject));
}
}, fail => {
if(ErrorCallBack != null)
ErrorCallBack(fail);
});
}
/// <summary>
/// Get request to the specified URL, optional CallBack and ErrorCallBack.
/// </summary>
/// <param name="File">File.</param>
/// <param name="CallBack">Call back.</param>
/// <param name="ErrorCallBack">Error call back.</param>
static public void Get(string URL, WWWCallBack CallBack = null, WWWErrorCallBack ErrorCallBack = null)
{
MHSOnline.NetworkAvailable(success => {
if(CallBack != null)
{
GameObject newGameObject = new GameObject("Co-routine (GET)");
newGameObject.AddComponent<MonoBehaviourEmpty>().StartCoroutine(MHSOnline.IRequest(URL, null, CallBack, newGameObject));
}
}, fail => {
if(ErrorCallBack != null)
ErrorCallBack(fail);
});
}
/// <summary>
/// Calls back success or failue
/// </summary>
/// <param name="SuccessCallBack">Success call back.</param>
/// <param name="ErrorCallBack">Error call back.</param>
static public void NetworkAvailable(WWWCallBack SuccessCallBack = null, WWWErrorCallBack ErrorCallBack = null)
{
NetworkReachability networkReachability = Application.internetReachability;
if(networkReachability == NetworkReachability.NotReachable)
ErrorCallBack("NetworkReachability.NotReachable");
GameObject newGameObject = new GameObject("Co-routine (Network)");
newGameObject.AddComponent<MonoBehaviourEmpty>().StartCoroutine(MHSOnline.INetworkAvailable(success => {
SuccessCallBack();
}, fail => {
ErrorCallBack(fail);
}, newGameObject));
}
// PRIVATE
static private IEnumerator INetworkAvailable(WWWCallBack SuccessCallBack = null, WWWErrorCallBack ErrorCallBack = null, GameObject TempObject = null)
{
WWW www = new WWW(MHSOnline.Domain+"/"+MHSOnline.DirectoryPath+"/"+MHSOnline.PingFile);
yield return www;
if(www.text == "true")
SuccessCallBack();
else
ErrorCallBack(www.error);
if(TempObject != null)
MonoBehaviour.Destroy(TempObject);
}
static private IEnumerator IRequest(string File, Dictionary<string, string> Data = null, WWWCallBack CallBack = null, GameObject TempObject = null)
{
WWW www;
if(Data != null)
{
WWWForm form = new WWWForm();
foreach(KeyValuePair<string, string> KV in Data)
form.AddField(KV.Key, KV.Value);
www = new WWW(MHSOnline.Domain+"/"+MHSOnline.DirectoryPath+"/"+File, form);
}
else
{
www = new WWW(MHSOnline.Domain+"/"+MHSOnline.DirectoryPath+"/"+File);
}
if(www != null)
yield return www;
if(CallBack != null)
{
string result = www.text;
CallBack(result);
if(TempObject != null)
MonoBehaviour.Destroy(TempObject);
}
}
}
}
thank you in advance ricky
Comment