Unity3d UnityWebRequest.downloadHandler.text为空,即使POST方法返回响应 我正在使用UnityWebRequest(我拥有的Unity版本是2017.4.0f1)对我的服务器进行api后调用 我将请求正文中的一些数据元素发送到服务器,服务器 插入我的数据库并返回一个响应体,它是一个json字符串 我正在使用 UnityWebRequest.downloadhandler.text读取响应消息, 但它是空的,即使是数据元素 正在被插入我的数据库。request.downloadHandler.data.Length也给我0 打同样的电话 邮递员会给我相应的回复(使用 HTTPWebRequest和通过流读取器读取响应)
这是我为它准备的代码片段:Unity3d UnityWebRequest.downloadHandler.text为空,即使POST方法返回响应 我正在使用UnityWebRequest(我拥有的Unity版本是2017.4.0f1)对我的服务器进行api后调用 我将请求正文中的一些数据元素发送到服务器,服务器 插入我的数据库并返回一个响应体,它是一个json字符串 我正在使用 UnityWebRequest.downloadhandler.text读取响应消息, 但它是空的,即使是数据元素 正在被插入我的数据库。request.downloadHandler.data.Length也给我0 打同样的电话 邮递员会给我相应的回复(使用 HTTPWebRequest和通过流读取器读取响应),unity3d,Unity3d,这是我为它准备的代码片段: UnityWebRequest request=new UnityWebRequest(endpoint,"POST"); request.SetRequestHeader("Content-Type","application/json"); request.SetRequestHeader("host",host); request.SetRequestHeader("X-Amz-Date",dateTime); request.SetRequestHeader(
UnityWebRequest request=new UnityWebRequest(endpoint,"POST");
request.SetRequestHeader("Content-Type","application/json");
request.SetRequestHeader("host",host);
request.SetRequestHeader("X-Amz-Date",dateTime);
request.SetRequestHeader("Authorization",authorizationHeader);
request.uploadHandler=(UploadHandler)new
UploadHandlerRaw(Encoding.UTF8.GetBytes(requestParameter));
request.chunkedTransfer=false;
request.downloadHandler=new DownloadHandlerBuffer();
request.SendWebRequest();
print(request.downloadHandler.text);
请告知我这里做错了什么….您需要等待结果下载,然后才能对其进行处理。Webrequests是异步的 通常,您会使用一个协同程序来实现这一点,比如
public IEnumerator LoadData()
{
// ......
// all your code goes here, up to the SendWebRequest line
// then you yield to wait for the request to return
yield return request.SendWebRequest();
// after this, you will have a result
print(request.downloadHandler.text);
}
按如下方式启动此协同程序:
StartCoroutine(LoadData());
这个问题的答案中有更多的例子:您需要等待结果被下载,然后才能对其执行任何操作。Webrequests是异步的 通常,您会使用一个协同程序来实现这一点,比如
public IEnumerator LoadData()
{
// ......
// all your code goes here, up to the SendWebRequest line
// then you yield to wait for the request to return
yield return request.SendWebRequest();
// after this, you will have a result
print(request.downloadHandler.text);
}
按如下方式启动此协同程序:
StartCoroutine(LoadData());
这个问题的答案中有更多的例子:正如frankhermes所说,在你做任何事情之前,你需要等待结果被下载
yield return request.SendWebRequest()代码>
但仅上述这一行对我来说并不奏效
public IEnumerator LoadData()
{
// ......
// all your code goes here, up to the SendWebRequest line
var asyncOperation = request.SendWebRequest();
while (!asyncOperation.isDone)
{
// wherever you want to show the progress:
float progress = request.downloadProgress;
Debug.Log("Loading " + progress);
yield return null;
}
while (!request.isDone)
yield return null; // this worked for me
// after this, you will have a result
print(request.downloadHandler.text);
}
正如frankhermes所说,在你做任何事情之前,你需要等待结果被下载
yield return request.SendWebRequest()代码>
但仅上述这一行对我来说并不奏效
public IEnumerator LoadData()
{
// ......
// all your code goes here, up to the SendWebRequest line
var asyncOperation = request.SendWebRequest();
while (!asyncOperation.isDone)
{
// wherever you want to show the progress:
float progress = request.downloadProgress;
Debug.Log("Loading " + progress);
yield return null;
}
while (!request.isDone)
yield return null; // this worked for me
// after this, you will have a result
print(request.downloadHandler.text);
}