WebProvider
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
public class WebProvider:MonoBehaviour
{
private static readonly JsonSerializerSettings jsonSettings=
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Populate};
public static WebProvider instance;
void Awake()
{
instance = this;
}
public void SendRequest<T>(WebRequest request, Action<WebResponse<T>> callback)
{
StartCoroutine(SendRequestCoroutine(request, callback));
}
private IEnumerator SendRequestCoroutine<T>(WebRequest request, Action<WebResponse<T>> callback)
{
UnityWebRequest unityWebRequest= request.Method == WebMethod.Get? UnityWebRequest.Get(request.Url):
UnityWebRequest.Post(request.Url, request.FormFields);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
{
callback?.Invoke(WebResponse<T>.NetworkError());
yield break;
}
WebResponse<T> response=null;
try
{
response = JsonConvert.DeserializeObject<WebResponse<T>>(unityWebRequest.downloadHandler.text, jsonSettings);
}
catch (Exception)
{
}
callback?.Invoke(response?? WebResponse<T>.DeserializeError());
}
}
WebMethod
public enum WebMethod
{
Get,
Post
}
WebRequest
using System;
using System.Collections.Generic;
public class WebRequest
{
private const string baseUrl = "https://www.xxxxx.com";
public Dictionary<string, string> FormFields { get; private set; } = new Dictionary<string, string>();
public string Url { get; private set; }
public WebMethod Method { get; private set; }
private WebRequest(WebMethod method,string url,Dictionary<string,object> formFields)
{
Method = method;
Url = baseUrl+url;
if (method == WebMethod.Get)
{
if (formFields.Count > 0)
{
Url += "?";
foreach (string key in formFields.Keys)
{
Url += string.Format("{0}={1}&", key, Convert.ToString(formFields[key]));
}
Url = Url.TrimEnd('&');
}
}
else
{
foreach (string key in formFields.Keys)
{
FormFields[key] = Convert.ToString(formFields[key]);
}
}
}
public static WebRequest login(string code)
{
var formFields = new Dictionary<string, object>();
formFields["code"] = code;
return new WebRequest(WebMethod.Get,"/login", formFields);
}
}
WebResponse
using System;
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class WebResponse<T>
{
private const int networkError= -1;
private const int deserializeError = -2;
[JsonProperty(PropertyName = "info")]
public string Message { get; private set; }
[JsonProperty(PropertyName = "status")]
public int Code { get; private set; }
public T Data { get; private set; }
//有构造函数时,默认值特性写在构造函数参数上,否则写在字段上
public WebResponse([DefaultValue(deserializeError)] int code,string message,T data)
{
Code = code;
Message = message;
Data = data==null? Activator.CreateInstance<T>(): data;
}
private WebResponse(int code)
{
Code = code;
}
public static WebResponse<T> NetworkError()
{
return new WebResponse<T>(networkError);
}
public static WebResponse<T> DeserializeError()
{
return new WebResponse<T>(deserializeError);
}
public bool isSuccessful()
{
return Code == 200;
}
}
网友评论