c# - 如何使用 RestClient for Unity 执行多个请求,然后在所有请求完成后执行一个函数?
问题描述
使用这个库https://github.com/proyecto26/RestClient我想从 s3 服务器获取多个图像,然后在它们全部下载后执行一个函数。我有:
RestClient.Get(url).Then( response => {
byte[] imageBytes = response.Data;
string pathToSaveImage = "/some/example/local/absolute/path/to/image.jpg";
File.WriteAllBytes(pathToSaveImage, imageBytes)
} ).Catch( err => {
Debug.Log(err.Message);
} );
但是如果我遍历一个 URL 列表,我怎么知道所有请求何时完成?
C# 和 Unity 的新手。任何帮助表示赞赏!谢谢!
解决方案
如果您必须使用“RestClient”,请使用正确的请求来获取照片。您可以使用以下代码:
var currentRequest = new RequestHelper {
Uri = "https://jsonplaceholder.typicode.com/photos",
Headers = new Dictionary<string, string> {
{ "Authorization", "Other token..." }
},
Params = new Dictionary<string, string> {
{ "param1", "Other value..." }
}
};
RestClient.GetArray<Photo>(currentRequest).Then(response => {
EditorUtility.DisplayDialog("Header", currentRequest.GetHeader("Authorization"), "Ok");
});
此外,如果您要下载多张照片,请尝试根据请求获取多张照片的 URL。
您可以通过以下方式了解状态:
currentRequest.UploadProgress; //The progress by uploading data to the server
currentRequest.UploadedBytes; //The number of bytes of body data the system has uploaded
currentRequest.DownloadProgress; //The progress by downloading data from the server
currentRequest.DownloadedBytes; //The number of bytes of body data the system has downloaded
currentRequest.Abort(); //Abort the request manually
推荐阅读
- android - 如果应用程序失去焦点,强制 android 应用程序完全重新启动
- php - Redux 框架 - 自定义字体过滤器
- sql - SQL Server 暗连接
- amazon-web-services - CloudWatchEvents.putRule() 上的 AccessDeniedException
- java - 为什么不能在 Java 中将 float[] 转换为 double[]?
- c# - 对运行时命中应用程序的 API 调用进行单元测试的最佳方法
- ember.js - 模型反序列化之前的页面加载
- reactjs - React Bootstrap Table 未显示从 api 响应中获取的数据
- php - 将变量传递给 CURLOPT_URL 时,Curl 返回错误的结果
- c++ - QString 和 QByteArray 内存使用情况