2016-07-14 28 views
3

我正在使用Unity創建HoloLens應用程序,該應用程序必須從REST API獲取數據並顯示它。 我目前使用WWW數據類型來獲取數據並在協程中產生返回語句,這將從Update()函數中調用。當我嘗試運行代碼時,我從API獲取最新數據,但是當有人將任何新數據推送到API上時,它不會自動實時獲取最新數據,並且我必須重新啓動應用才能看到最新數據。 我的代碼:WWW/UnityWebRequest POST/GET請求不會從服務器/ url返回最新數據

using UnityEngine; 
using UnityEngine.UI; 
using System.Collections; 
using System; 
using Newtonsoft.Json; 
using System.Collections.Generic; 
using System.IO; 

public class TextChange : MonoBehaviour { 

    // Use this for initialization 
    WWW get; 
    public static string getreq; 
    Text text; 
    bool continueRequest = false; 

    void Start() 
    { 
     StartCoroutine(WaitForRequest()); 
     text = GetComponent<Text>(); 
    } 

    // Update is called once per frame 
    void Update() 
    { 

    } 

    private IEnumerator WaitForRequest() 
    { 
     if (continueRequest) 
      yield break; 

     continueRequest = true; 

     float requestFrequencyInSec = 5f; //Update after every 5 seconds 
     WaitForSeconds waitTime = new WaitForSeconds(requestFrequencyInSec); 

     while (continueRequest) 
     { 
      string url = "API Link goes Here"; 
      WWW get = new WWW(url); 
      yield return get; 
      getreq = get.text; 
      //check for errors 
      if (get.error == null) 
      { 
       string json = @getreq; 
       List<MyJSC> data = JsonConvert.DeserializeObject<List<MyJSC>>(json); 
       int l = data.Count; 
       text.text = "Data: " + data[l - 1].content; 
      } 
      else 
      { 
       Debug.Log("Error!-> " + get.error); 
      } 

      yield return waitTime; //Wait for requestFrequencyInSec time 
     } 
    } 

    void stopRequest() 
    { 
     continueRequest = false; 
    } 
} 

public class MyJSC 
{ 
    public string _id; 
    public string author; 
    public string content; 
    public string _v; 
    public string date; 
} 
+0

就像你在你的問題你不應該要求在更新功能協同程序功能。這就像在一秒鐘內完成60多個請求。我已經在你的問題中解決了這個問題,用等待的代碼替換它,然後再次發出請求。如果這不能解決您的問題,請參閱我的答案。 – Programmer

+0

你試過解決方案嗎? – Programmer

+1

是的,它像一個魅力.... –

回答

5

這是發生,因爲資源緩存在服務器上啓用。

三種可能的解決方案我知道

Disable資源緩存在服務器。每個Web服務器的Instructions都不相同。通常在.htaccess完成。

。使用唯一的時間戳創建每個請求。時間應採用Unix格式。

此方法不適用於iOS。你很好,因爲這是HoloLens

例如,如果您的網址是http://url.com/file.rar,請在末尾追加?t=currentTimecurrentTimeUnix格式中的實際時間。

完整的示例網址:http://url.com/file.rar?t=1468475141

代碼

string getUTCTime() 
{ 
    System.Int32 unixTimestamp = (System.Int32)(System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1))).TotalSeconds; 
    return unixTimestamp.ToString(); 
} 

private IEnumerator WaitForRequest() 
{ 
    string url = "API Link goes Here" + "?t=" + getUTCTime(); 
    WWW get = new WWW(url); 
    yield return get; 
    getreq = get.text; 
    //check for errors 
    if (get.error == null) 
    { 
     string json = @getreq; 
     List<MyJSC> data = JsonConvert.DeserializeObject<List<MyJSC>>(json); 
     int l = data.Count; 
     text.text = "Data: " + data[l - 1].content; 
    } 
    else 
    { 
     Debug.Log("Error!-> " + get.error); 
    } 
} 

Disable通過提供並修改請求中的Cache-ControlPragma標頭,在客戶端端緩存。

Cache-Controlmax-age=0, no-cache, no-store然後設置Pragmano-cache

我建議你用UnityWebRequest而不是WWW這樣做。首先,包括using UnityEngine.Networking;

代碼

IEnumerator WaitForRequest(string url) 
{ 

    UnityWebRequest www = UnityWebRequest.Get(url); 
    www.SetRequestHeader("Cache-Control", "max-age=0, no-cache, no-store"); 
    www.SetRequestHeader("Pragma", "no-cache"); 

    yield return www.Send(); 
    if (www.isError) 
    { 
     Debug.Log(www.error); 
    } 
    else 
    { 
     Debug.Log("Received " + www.downloadHandler.text); 
    } 
} 
+0

作爲便箋,您還應該在更新中解決呼叫。目前,每幀有幾十個協程。 InvokeRepeating更合適。 – Everts

+0

@Everts只是注意到,你是正確的。我的答案已經很長了,我不想再延長它。我修改了他的答案,以解決這個問題,並在他的問題下發表評論,讓他知道這一點。請注意,您不能在協同程序中使用所有類型的Unity調用函數。 'while while循環似乎適合於此。檢查他的問題中更新的代碼,看看我的意思。 – Programmer

+0

做一個void方法來啓動協程,然後你可以使用InvokeRepeating。 – Everts

相關問題