WWW / UnityWebRequest POST / GET请求不会从服务器/ url返回最新数据 [英] WWW/UnityWebRequest POST/GET request won't return the latest data from server/url
问题描述
我使用Unity创建一个HoloLens应用程序,它必须从REST API获取数据并显示它。
我目前使用WWW数据类型来获取数据并在协程中产生返回语句,该语句将从Update()函数调用。当我尝试运行代码时,我从API获取最新数据,但是当有人将任何新数据推入API时,它不会自动实时获取最新数据,而必须重新启动该应用程序可以查看最新的数据。
我的代码:
使用UnityEngine;
使用UnityEngine.UI;
使用System.Collections;
使用System;
使用Newtonsoft.Json;
使用System.Collections.Generic;
使用System.IO;
public class TextChange:MonoBehaviour {
//使用它初始化
WWW get;
public static string getreq;
文本文本;
bool continueRequest = false;
void Start()
{
StartCoroutine(WaitForRequest());
text = GetComponent< Text>();
}
//更新每帧调用一次
void Update()
{
}
private IEnumerator WaitForRequest()
{
if(continueRequest)
yield break;
continueRequest = true;
float requestFrequencyInSec = 5f; //每5秒更新一次
WaitForSeconds waitTime = new WaitForSeconds(requestFrequencyInSec);
while(continueRequest)
{
string url =API链接到此处;
WWW get = new WWW(url);
产量回报得到;
getreq = get.text;
//检查错误
if(get.error == null)
{
string json = @getreq;
列表< 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; //等待requestFrequencyInSec时间
}
}
void stopRequest()
{
continueRequest = false;
}
}
公共类MyJSC
{
公共字符串_id;
公共字符串作者;
公共字符串内容;
public string _v;
公共字符串日期;
这是因为资源缓存已在服务器上启用。
我知道的三种可能的解决方案:
1 。在服务器上禁用
资源缓存。对于每个Web服务器,说明都不同。通常在 .htaccess
中完成。
2 。使用唯一时间戳创建每个请求。时间应该在 Unix 格式。
这个方法不适用于iOS 。因为这是 HoloLens
。
例如,如果您的网址是 http://url.com/file.rar
,最后追加?t = currentTime
。 currentTime
是 Unix 格式中的实际时间。
完整示例网址: http://url.com/file.rar?t=1468475141
$ b 代码:
string getUTCTime()
{
System.Int32 unixTimestamp =(System.Int32)(System.DateTime.UtcNow.Subtract(new System.DateTime(1970,1,1)))。TotalSeconds;
return unixTimestamp.ToString();
$ b私有IEnumerator WaitForRequest()
{
string url =API链接到此处+?t =+ getUTCTime();
WWW get = new WWW(url);
产量回报得到;
getreq = get.text;
//检查错误
if(get.error == null)
{
string json = @getreq;
列表< MyJSC> data = JsonConvert.DeserializeObject< List< MyJSC>>(json);
int l = data.Count;
text.text =Data:+ data [l - 1] .content;
}
else
{
Debug.Log(Error! - >+ get.error);
3 。<$通过提供和修改 Cache-Control
和 Disable
缓存客户端 c> Pragma 请求中的标题。
将缓存控制
标题设为 max-age = 0,no-cache,no-store
然后设置 Pragma
标题改为 no-缓存
。
我建议你用 UnityWebRequest
代替 WWW
类。首先,使用UnityEngine.Networking包含;
。
$ b 代码:
IEnumerator WaitForRequest (字符串url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
www.SetRequestHeader(Cache-Control,max-age = 0,no-cache,no-store);
www.SetRequestHeader(Pragma,no-cache);
收益回报www.Send();
if(www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(Received+ www.downloadHandler.text);
}
}
I am creating a HoloLens app using Unity which has to take data from a REST API and display it. I am currently using WWW datatype to get the data and yield return statement in a coroutine that will be called from the Update() function. When I try to run the code, I get the latest data from the API but when someone pushes any new data onto the API, it does not automatically get the latest data in real time and I have to restart the app to see the latest data. My Code:
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;
}
This is happening because resources caching is enabled on the Server.
Three possible solutions I know about:
1.Disable
resources caching on the server. Instructions are different for every web server. Usually done in .htaccess
.
2.Make each request with unique timestamp. The time should in Unix format.
This method will not work on iOS. You are fine since this is for HoloLens
.
For example, if your url is http://url.com/file.rar
, append ?t=currentTime
at the end. currentTime
is the actual time in Unix Format.
Full example url: http://url.com/file.rar?t=1468475141
Code:
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);
}
}
3.Disable
Cache on the client side by supplying and modifying the Cache-Control
and Pragma
headers in the request.
Set Cache-Control
header to max-age=0, no-cache, no-store
then set Pragma
header to no-cache
.
I suggest you do this with UnityWebRequest
instead of the WWW
class. First, Include using UnityEngine.Networking;
.
Code:
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);
}
}
这篇关于WWW / UnityWebRequest POST / GET请求不会从服务器/ url返回最新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!