testDemo.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using LitJson;
  8. public class testDemo : MonoBehaviour
  9. {
  10. public Text ttime;
  11. public Text tfct;
  12. public Text tfzct;
  13. public Text tjzct;
  14. // Start is called before the first frame update
  15. void Start()
  16. {
  17. ttime.text = DateTime.Now.ToString();
  18. sendtest();
  19. }
  20. public void load()
  21. {
  22. }
  23. public void sendtest()
  24. {
  25. StartCoroutine(SendHttp("cmcc-endustry/v1/report/dairyCattle/total", new Dictionary<string, string>(), (string msg) => {
  26. JsonData data = JsonMapper.ToObject(msg);
  27. int ct = int.Parse(data["data"]["list"]["001"].ToString())%14;
  28. Debug.Log("msg===>" + msg);
  29. int i = ct % 7;
  30. tfct.text = i+"/7";
  31. tfzct.text = (ct / 7).ToString();
  32. tjzct.text = "2";
  33. Invoke("sendtest",1f);
  34. }));
  35. }
  36. public IEnumerator SendHttp(string methodName, Dictionary<string, string> fromDic, Action<string> CallBack)
  37. {
  38. Debug.Log("Start Queue SendHttp " + methodName);
  39. string url = "https://api-fat2.ghz-tech.com/" + methodName;
  40. UnityWebRequest webRequest;
  41. WWWForm form = new WWWForm();
  42. foreach (var item in fromDic)
  43. {
  44. form.AddField(item.Key, item.Value);
  45. }
  46. webRequest = UnityWebRequest.PostWwwForm(url, "Post");
  47. webRequest.SetRequestHeader("Content-Type", "application/json");
  48. webRequest.SetRequestHeader("Authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjMxNTM2MDAxNzEyNzQ0NjUxLCJpYXQiOjE3MTI3NDQ2NTEsInVzZXIiOnsiaWQiOjExLCJlbWFpbCI6IiIsInBob25lIjoieWIzIiwibmFtZSI6InliMyIsInJvbGVJZCI6M319.WGpVJHfbTr4Bp2cySjjlgCIon8nF4sJxkl58wcIion8");
  49. yield return webRequest.SendWebRequest();
  50. if (webRequest.result == UnityWebRequest.Result.ConnectionError)
  51. {
  52. Debug.LogError(webRequest.error);
  53. }
  54. else
  55. {
  56. string result = webRequest.downloadHandler.text;
  57. CallBack(result);
  58. }
  59. }
  60. // Update is called once per frame
  61. void Update()
  62. {
  63. }
  64. }