问题描述
我已经编写了与Indy IdHTTP组件一起使用get的代码
I have written code to use get with indy IdHTTP component
var
get_url: string;
resp: TMemoryStream;
begin
get_url := 'http://localhost/salesapp/is_run.php?ser=';
resp := TMemoryStream.Create;
IdHTTP1.Get(get_url+'v', resp);
memo1.Lines.LoadFromStream(resp);
此网址 http://localhost/salesapp/is_run.php?ser = v
返回JSON响应,但我不知道如何从Delphi中读取它。
This url http://localhost/salesapp/is_run.php?ser=v
return JSON response but I dont know how to read it from Delphi.
推荐答案
何时 Get()
退出,流的 Position
在流的末尾。您需要在调用 LoadFromStream()
之前将 Position
重置为0,否则它将没有任何内容可加载:
When Get()
exits, the stream's Position
is at the end of the stream. You need to reset the Position
back to 0 before calling LoadFromStream()
, or else it will not have anything to load:
var
get_url: string;
resp: TMemoryStream;
begin
get_url := 'http://localhost/salesapp/is_run.php?ser=';
resp := TMemoryStream.Create;
try
IdHTTP1.Get(get_url+'v', resp);
resp.Position := 0; // <-- add this!!
memo1.Lines.LoadFromStream(resp);
finally
resp.Free;
end;
end;
另一种方法是删除 TMemoryStream
并让 Get()
以 String
的形式返回JSON:
The alternative is to remove the TMemoryStream
and let Get()
return the JSON as a String
instead:
memo1.Text := IdHTTP1.Get(get_url+'v');
这篇关于如何从Indy IdHTTP获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!