如何调用RESTRequest.ExecuteAsync
How to call RESTRequest.ExecuteAsync
我正在尝试使用在运行时创建的组件发出异步 Web 请求。但我无法让 ExecuteAsync 方法工作:它引发错误:请求没有客户端组件
重现方法如下:
在空表格上放置备忘录,然后输入此代码:
uses
REST.Types, REST.Client;
procedure TForm65.FormCreate(Sender: TObject);
var
RESTClient: TRESTClient;
RESTResponse: TRESTResponse;
RESTRequest: TRESTRequest;
begin
ReportMemoryLeaksOnShutdown := True;
RESTClient := TRESTClient.Create('https://dawa.aws.dk/postnumre?nr=9310');
RESTResponse := TRESTResponse.Create(Self);
RESTRequest := TRESTRequest.Create(RESTClient);
RESTRequest.Response := RESTResponse;
RESTRequest.Execute;
Memo1.Lines.Add(RESTResponse.Content);
RESTRequest.ExecuteAsync(
procedure
begin
Memo1.Lines.Text := RESTResponse.Content
end, True, True,
procedure(AObject: TObject)
begin
Memo1.Lines.Add('Error: ' + ERESTException(AObject).Message);
end);
FreeAndNil(RESTClient);
end;
第一个同步调用只是为了表明我从该调用中获取了数据。
那我做错了什么?
Dalija Prasnikar 为我指出了正确的方向:因为请求是异步执行的,所以 FreeAndNil 可能会 运行 在请求执行之前。这将释放客户端以及请求,因为客户端拥有请求。
只是有人对我说我没有设置 TRESTRequest.Client 属性。那是不正确的!由于客户端是请求的所有者,因此客户端 属性 位于构造函数中。
{ TCustomRESTRequest }
constructor TCustomRESTRequest.Create(AOwner: TComponent);
begin
// it is important to create the notify-list before
// calling the inherited constructor
FNotifyList := TNotifyList.Create;
inherited Create(AOwner);
// if we do get a client as owner, we just use
// it. otherwise we look around and try to find
// an existing client (maybe on the same form)
if AOwner is TCustomRESTClient then
Client := TCustomRESTClient(AOwner)
else if RESTComponentIsDesigning(Self) then
Client := RESTFindDefaultClient(Self);
...
我正在尝试使用在运行时创建的组件发出异步 Web 请求。但我无法让 ExecuteAsync 方法工作:它引发错误:请求没有客户端组件
重现方法如下:
在空表格上放置备忘录,然后输入此代码:
uses
REST.Types, REST.Client;
procedure TForm65.FormCreate(Sender: TObject);
var
RESTClient: TRESTClient;
RESTResponse: TRESTResponse;
RESTRequest: TRESTRequest;
begin
ReportMemoryLeaksOnShutdown := True;
RESTClient := TRESTClient.Create('https://dawa.aws.dk/postnumre?nr=9310');
RESTResponse := TRESTResponse.Create(Self);
RESTRequest := TRESTRequest.Create(RESTClient);
RESTRequest.Response := RESTResponse;
RESTRequest.Execute;
Memo1.Lines.Add(RESTResponse.Content);
RESTRequest.ExecuteAsync(
procedure
begin
Memo1.Lines.Text := RESTResponse.Content
end, True, True,
procedure(AObject: TObject)
begin
Memo1.Lines.Add('Error: ' + ERESTException(AObject).Message);
end);
FreeAndNil(RESTClient);
end;
第一个同步调用只是为了表明我从该调用中获取了数据。
那我做错了什么?
Dalija Prasnikar 为我指出了正确的方向:因为请求是异步执行的,所以 FreeAndNil 可能会 运行 在请求执行之前。这将释放客户端以及请求,因为客户端拥有请求。
只是有人对我说我没有设置 TRESTRequest.Client 属性。那是不正确的!由于客户端是请求的所有者,因此客户端 属性 位于构造函数中。
{ TCustomRESTRequest }
constructor TCustomRESTRequest.Create(AOwner: TComponent);
begin
// it is important to create the notify-list before
// calling the inherited constructor
FNotifyList := TNotifyList.Create;
inherited Create(AOwner);
// if we do get a client as owner, we just use
// it. otherwise we look around and try to find
// an existing client (maybe on the same form)
if AOwner is TCustomRESTClient then
Client := TCustomRESTClient(AOwner)
else if RESTComponentIsDesigning(Self) then
Client := RESTFindDefaultClient(Self);
...