unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, ExtCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
IdHTTP1: TIdHTTP;
edt1: TEdit;
edt2: TEdit;
btn2: TButton;
tmr1: TTimer;
procedure btn1Click(Sender: TObject);
procedure edt1KeyPress(Sender: TObject; var Key: Char);
procedure btn2Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
private
{ Private declarations }
public
function GetResponseContentEx(const http: TIdHTTP; url: string;
param: TStringStream=nil): string;
function getResult(sl:TStrings):string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetResponseContentEx(const http: TIdHTTP; url: string;
param: TStringStream=nil): string;
var
response: TStringStream;
begin
result := '';
response := TStringStream.Create('');
if param=nil then
begin
http.Get(url,response);
end
else begin
http.Post(url,param,response);
end;
result := response.DataString;
response.Free;
end;
//获取 股票 千山药机 的 股价
//http://hq.sinajs.cn/list=sz300216
procedure TForm1.btn1Click(Sender: TObject);
var
url,s:string;
sl:TStrings;
currentPrice,yesterdayPrice,increasePercent:Double;
begin
url:= 'http://hq.sinajs.cn/list=sz300216';
s:= GetResponseContentEx(IdHTTP1,url,nil);
sl:= TStringList.Create;
try
sl.Delimiter:=',';
sl.DelimitedText:= s;
//ShowMessage(sl[4]); //当前价
//ShowMessage(sl[3]); //昨收
edt1.Text:='qs'+getResult(sl);
url:= 'http://hq.sinajs.cn/list='+'sz399006';
s:= GetResponseContentEx(IdHTTP1,url,nil);
sl.DelimitedText:= s;
edt2.Text:='c'+getResult(sl);
finally
sl.free;
end;
end;
function TForm1.getResult(sl: TStrings): string;
var
currentPrice,yesterdayPrice,increasePercent:Double;
begin
result:= '';
if sl=nil then exit;
if (sl=nil)or(sl.Count<4) then Exit;
currentPrice:= StrToFloat(sl[4]);
yesterdayPrice:= StrToFloat(sl[3]);
if currentPrice>=yesterdayPrice then
result:='+++++'+FloatToStr( ((currentPrice-yesterdayPrice)/yesterdayPrice)*100)
else
result:='-----'+ FloatToStr( ((yesterdayPrice-currentPrice)/yesterdayPrice)*100);
// if currentPrice>=yesterdayPrice then
// increasePercent:= ((currentPrice-yesterdayPrice)/yesterdayPrice)*100
// else
// increasePercent:= -((yesterdayPrice-currentPrice)/yesterdayPrice)*100;
//
// if increasePercent =0 then
// Result:= '0'
// else
// Result:= Format('%.2f%s',[increasePercent,'%']);
end;
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
btn1Click(btn1);
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.tmr1Timer(Sender: TObject);
begin
btn1Click(btn1);
end;
end.