393 lines
13 KiB
C#
393 lines
13 KiB
C#
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Net.Http;
|
||
using System.Net.Http.Headers;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
|
||
/// <summary>
|
||
///整合JSON
|
||
/// </summary>
|
||
public static class CombineJSON
|
||
{
|
||
//public static List<string> jsons = new List<string>();
|
||
public static Dictionary<string, string> jsonsDic = new Dictionary<string, string>();
|
||
|
||
#region 直连
|
||
/// <summary>
|
||
/// 获取所有设备API
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static string GetCombineJSON()
|
||
{
|
||
//创建一个JArray对象,用于存储多个JSON 对象
|
||
JArray jsonArray = new JArray();
|
||
|
||
// 解析JSON字符串,并添加到JArray
|
||
foreach (var item in jsonsDic.Values)
|
||
{
|
||
var jsonObj = JObject.Parse(item);
|
||
jsonArray.Add(jsonObj);
|
||
}
|
||
|
||
//创建一个包含JArray的JObject对象
|
||
JObject resultObj = new JObject();
|
||
resultObj["data"] = jsonArray;
|
||
|
||
//将整合后的JSON输出成字符串
|
||
string resultJson = resultObj.ToString();
|
||
return resultJson;
|
||
}
|
||
|
||
/// <summary>
|
||
/// POST修改数据的请求
|
||
/// </summary>
|
||
/// <param name="apiUrl"></param>
|
||
/// <param name="newData"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
public static async Task<string> UpdateJson_POST(string apiUrl, string token, string newData)
|
||
{
|
||
var client = new HttpClient();
|
||
var request = new HttpRequestMessage
|
||
{
|
||
Method = HttpMethod.Post,
|
||
RequestUri = new Uri(apiUrl),
|
||
Headers =
|
||
{
|
||
{ "X-Token", token },
|
||
},
|
||
Content = new StringContent(newData)
|
||
{
|
||
Headers =
|
||
{
|
||
ContentType = new MediaTypeHeaderValue("application/json")
|
||
}
|
||
}
|
||
};
|
||
using (var response = await client.SendAsync(request))
|
||
{
|
||
response.EnsureSuccessStatusCode();
|
||
var body = await response.Content.ReadAsStringAsync();
|
||
return body;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// POST获取数据的请求
|
||
/// </summary>
|
||
/// <param name="apiUrl"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
public static async Task<string> GetJson_POST(string apiUrl, string token)
|
||
{
|
||
using (HttpClient httpClient = new HttpClient())
|
||
{
|
||
Debug.Log(apiUrl + "=aaaaaa=");
|
||
var client = new HttpClient();
|
||
var request = new HttpRequestMessage
|
||
{
|
||
Method = HttpMethod.Post,
|
||
RequestUri = new Uri(apiUrl),
|
||
Headers =
|
||
{
|
||
{ "X-Token", token },
|
||
},
|
||
};
|
||
using (var response = await client.SendAsync(request))
|
||
{
|
||
Debug.Log(apiUrl + "=bbbbbbb=");
|
||
|
||
response.EnsureSuccessStatusCode();
|
||
var body = await response.Content.ReadAsStringAsync();
|
||
Debug.Log(body);
|
||
return (body);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取GET接口
|
||
/// </summary>
|
||
/// <param name="apiUrl"></param>
|
||
/// <returns></returns>
|
||
public static async Task<string> GetJson_GET(string apiUrl)
|
||
{
|
||
using (var httpClient = new HttpClient())
|
||
{
|
||
using (var response = await httpClient.GetAsync(apiUrl))
|
||
{
|
||
if (response.IsSuccessStatusCode)
|
||
{
|
||
string jsonResult = await response.Content.ReadAsStringAsync();
|
||
// 处理返回的JSON数据
|
||
Debug.Log(jsonResult);
|
||
return jsonResult;
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("请求GET接口失败: " + response.StatusCode);
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 协程调用接口
|
||
|
||
|
||
/// <summary>
|
||
/// POST获取数据的请求
|
||
/// </summary>
|
||
/// <param name="apiUrl"></param>
|
||
/// <param name="token"></param>
|
||
/// <param name="callback"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator GetJson_POST(string apiUrl, string token, Action<string> callback)
|
||
{
|
||
//using (UnityWebRequest request = UnityWebRequest.Post(apiUrl, ""))
|
||
UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");
|
||
{
|
||
request.SetRequestHeader("X-Token", token);
|
||
request.SetRequestHeader("Content-Type", "application/json");
|
||
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
|
||
request.downloadHandler = dH;
|
||
//request.timeout = 15;
|
||
yield return request.SendWebRequest();
|
||
|
||
if (!request.isNetworkError && !request.isHttpError)
|
||
{
|
||
// 等待一帧
|
||
yield return null;
|
||
|
||
// 检查请求是否已完成
|
||
while (!request.isDone)
|
||
{
|
||
yield return null;
|
||
}
|
||
//if (request.downloadHandler.text != null)
|
||
if (!string.IsNullOrEmpty(request.downloadHandler.text))
|
||
//byte[] responseData = request.downloadHandler.data;
|
||
//if (responseData != null && responseData.Length > 0)
|
||
{
|
||
string responseText = request.downloadHandler.text;
|
||
//Debug.Log(responseText);
|
||
//string responseText = Encoding.UTF8.GetString(responseData);
|
||
callback(responseText);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(apiUrl + " request.downloadHandler.text为null");
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(apiUrl + " 请求POST接口失败: " + request.error + "\nURL: " + apiUrl + "\nrequest.downloadHandler:" + request.downloadHandler.text);
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// POST修改数据的请求
|
||
/// </summary>
|
||
/// <param name="apiUrl"></param>
|
||
/// <param name="token"></param>
|
||
/// <param name="newData"></param>
|
||
/// <param name="onComplete"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator UpdateJson_POST(string apiUrl, string token, string newData, Action<string> callback)
|
||
{
|
||
//using (UnityWebRequest request = new UnityWebRequest(apiUrl, "POST"))
|
||
UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");
|
||
{
|
||
request.SetRequestHeader("X-Token", token);
|
||
request.SetRequestHeader("Content-Type", "application/json");
|
||
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(newData);
|
||
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
|
||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||
request.downloadHandler = new DownloadHandlerBuffer();
|
||
request.downloadHandler = dH;
|
||
//request.timeout = 15;
|
||
yield return request.SendWebRequest();
|
||
|
||
if (request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
// 等待一帧
|
||
yield return null;
|
||
|
||
// 检查请求是否已完成
|
||
while (!request.isDone)
|
||
{
|
||
yield return null;
|
||
}
|
||
//if (request.downloadHandler.text != null)
|
||
if (!string.IsNullOrEmpty(request.downloadHandler.text))
|
||
//byte[] responseData = request.downloadHandler.data;
|
||
//if (responseData != null && responseData.Length > 0)
|
||
{
|
||
string responseText = request.downloadHandler.text;
|
||
//string responseText = Encoding.UTF8.GetString(responseData);
|
||
callback?.Invoke(responseText);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(apiUrl + " request.downloadHandler.text为空");
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(apiUrl + " 请求POST接口失败: " + request.error + "\nURL: " + apiUrl);
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取GET接口
|
||
/// </summary>
|
||
/// <param name="apiUrl"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator GetJson_GET(string apiUrl, Action<string> callback)
|
||
{
|
||
using (UnityWebRequest request = UnityWebRequest.Get(apiUrl))
|
||
{
|
||
yield return request.SendWebRequest();
|
||
|
||
if (!request.isNetworkError && !request.isHttpError)
|
||
{
|
||
if (request.downloadHandler.text != null)
|
||
{
|
||
string jsonResult = request.downloadHandler.text;
|
||
//Debug.Log(jsonResult);
|
||
callback?.Invoke(jsonResult);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("request.downloadHandler.text为null");
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("请求GET接口失败: " + request.error);
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
}
|
||
|
||
public static IEnumerator GetJson_GET(string apiUrl, string token, Action<string> callback)
|
||
{
|
||
//using (UnityWebRequest request = UnityWebRequest.Get(apiUrl))
|
||
UnityWebRequest request = UnityWebRequest.Get(apiUrl);
|
||
{
|
||
request.SetRequestHeader("X-Token", token);
|
||
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
|
||
request.downloadHandler = dH;
|
||
//request.timeout = 15;
|
||
yield return request.SendWebRequest();
|
||
|
||
if (request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
// 等待一帧
|
||
yield return null;
|
||
|
||
// 检查请求是否已完成
|
||
while (!request.isDone)
|
||
{
|
||
yield return null;
|
||
}
|
||
//string responseBody = request.downloadHandler.text;
|
||
//if (request.downloadHandler.text != null)
|
||
if (!string.IsNullOrEmpty(request.downloadHandler.text))
|
||
// byte[] responseData = request.downloadHandler.data;
|
||
//if (responseData != null && responseData.Length > 0)
|
||
{
|
||
string responseText = request.downloadHandler.text;
|
||
//string responseText = Encoding.UTF8.GetString(responseData);
|
||
callback?.Invoke(responseText);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(apiUrl + " request.downloadHandler.text为空");
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(apiUrl + " 请求Get接口失败: " + request.error + "\nURL: " + apiUrl);
|
||
callback.Invoke(null);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public static IEnumerator GetJson_POST(string apiUrl, string token, int looptime, Action<string> callback)
|
||
{
|
||
while (true)
|
||
{
|
||
//if (CabinetUIManager.Instance.SomeMethod() && GameManager.Inst.isLoading)
|
||
//{
|
||
// yield return new WaitForSeconds(looptime);
|
||
//}
|
||
//else
|
||
//{
|
||
// yield return null;
|
||
//}
|
||
|
||
//using (UnityWebRequest request = UnityWebRequest.Post(apiUrl, ""))
|
||
UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");
|
||
{
|
||
request.SetRequestHeader("X-Token", token);
|
||
request.SetRequestHeader("Content-Type", "application/json");
|
||
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
|
||
request.downloadHandler = dH;
|
||
request.timeout = 15;
|
||
yield return request.SendWebRequest();
|
||
|
||
if (!request.isNetworkError && !request.isHttpError)
|
||
{
|
||
// 等待一帧
|
||
yield return null;
|
||
|
||
// 检查请求是否已完成
|
||
while (!request.isDone)
|
||
{
|
||
yield return null;
|
||
}
|
||
//if (request.downloadHandler.text != null)
|
||
if (!string.IsNullOrEmpty(request.downloadHandler.text))
|
||
// byte[] responseData = request.downloadHandler.data;
|
||
//if (responseData != null && responseData.Length > 0)
|
||
{
|
||
string responseText = request.downloadHandler.text;
|
||
//Debug.Log(responseText);
|
||
//string responseText = Encoding.UTF8.GetString(responseData);
|
||
callback(responseText);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("request.downloadHandler.text为null");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("请求POST接口失败: " + request.error + "\nURL: " + apiUrl);
|
||
}
|
||
}
|
||
yield return new WaitForSeconds(looptime);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
}
|