中國天氣網有一個連接http://m.weather.com.cn/data/+‘城市編碼.html‘,這個連接可以獲取對應的城市的天氣情況!返回Json數(shù)據(jù),解析Json就能獲得該城市的天氣!這個問題就在那個城市編碼的獲取方式上,這個獲取方式,網上有代碼的!我就不寫了,我是直接將城市編碼都包到資源文件中去了使用的時候,直接從資源加載就行了!獲得天氣的Json數(shù)據(jù),用的事Http協(xié)議,這里只要用可以提交http的數(shù)據(jù)的控件或者類都可以(比如,indy的idhttp,ics的http控件都行,也可以使用MSxml的IXMLHttpRequest,也可以使用HttpRequest5.1或者使用WinHttp控件),這里我用的事最簡單的方式,就用了Delphi直接帶的indy的idhttp來獲得
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,msxml,uLkJSON, ExtCtrls, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP;
type
TComboBox = class(StdCtrls.TComboBox)
private
Values: TStringList;
public
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
procedure LoadFromFile(Filename: string);
procedure LoadFromRes(ResName: string);
end;
TForm4 = class(TForm)
Memo1: TMemo;
Panel1: TPanel;
ComboBox1: TComboBox;
Button1: TButton;
IdHTTP1: TIdHTTP;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function pro_result(str:string):string ;
public
{ Public declarations }
List: TStringList;
HttpReq: IXMLHttpRequest;
end;
var
Form4: TForm4;
implementation
uses DateUtils;
{$R *.dfm}
{$R mm.RES}
procedure TForm4.Button1Click(Sender: TObject);
var
url: string;
Json: TlkJSONobject;
ChildJson,tmpJson: TlkJSONbase;
begin
url := ‘http://m.weather.com.cn/data/‘+Combobox1.Values.ValueFromIndex[ComboBox1.ItemIndex]+‘.html‘;
HttpReq.open(‘Get‘, Url, False, EmptyParam, EmptyParam);
HttpReq.send(EmptyParam);//開始搜索
Url := HttpReq.responseText;
Json := Tlkjson.ParseText(url) as TlkJSONobject;
ChildJson := Json.Field[‘weatherinfo‘];
Memo1.Lines.Clear;
if ChildJson.SelfType = jsObject then
begin
//Showmessage(ChildJson.Field[‘city‘].Value);
Memo1.Lines.Add(‘今日天氣(‘+Vartostr(ChildJson.Field[‘date_y‘].Value)+‘ ‘+Vartostr(ChildJson.Field[‘week‘].Value)+‘):‘);
Memo1.Lines.Add(‘ 溫度:‘+Vartostr(ChildJson.Field[‘temp1‘].Value));
Memo1.Lines.Add(‘ 天氣:‘+Vartostr(ChildJson.Field[‘weather1‘].Value));
//Memo1.Lines.Add(‘ 風向:‘+Vartostr(ChildJson.Field[‘fx1‘].Value)+ ‘ ‘+Vartostr(ChildJson.Field[‘wind1‘].Value));
Memo1.Lines.Add(‘ 風力:‘+Vartostr(ChildJson.Field[‘wind1‘].Value));
Memo1.Lines.Add(‘明日天氣(‘+FormatDateTime(‘YYYY年MM月DD日 ‘,DateUtils.IncDay(now))+‘):‘);
Memo1.Lines.Add(‘ 溫度:‘+Vartostr(ChildJson.Field[‘temp2‘].Value));
Memo1.Lines.Add(‘ 天氣:‘+Vartostr(ChildJson.Field[‘weather2‘].Value));
//Memo1.Lines.Add(‘ 風向:‘+Vartostr(ChildJson.Field[‘fx2‘].Value)+ ‘ ‘+Vartostr(ChildJson.Field[‘wind2‘].Value));
Memo1.Lines.Add(‘ 風力:‘+Vartostr(ChildJson.Field[‘wind2‘].Value));
Memo1.Lines.Add(FormatDateTime(‘YYYY年MM月DD日 ‘,DateUtils.IncDay(now,2))+‘:‘);
Memo1.Lines.Add(‘ 溫度:‘+Vartostr(ChildJson.Field[‘temp3‘].Value));
Memo1.Lines.Add(‘ 天氣:‘+Vartostr(ChildJson.Field[‘weather3‘].Value));
//if True then
//Memo1.Lines.Add(‘ 風向:‘+Vartostr(ChildJson.Field[‘fx3‘].Value)+ ‘ ‘+Vartostr(ChildJson.Field[‘wind3‘].Value));
Memo1.Lines.Add(‘ 風力:‘+Vartostr(ChildJson.Field[‘wind3‘].Value));
Memo1.Lines.Add(FormatDateTime(‘YYYY年MM月DD日 ‘,DateUtils.IncDay(now,3))+‘:‘);
Memo1.Lines.Add(‘ 溫度:‘+Vartostr(ChildJson.Field[‘temp4‘].Value));
Memo1.Lines.Add(‘ 天氣:‘+Vartostr(ChildJson.Field[‘weather4‘].Value));
//Memo1.Lines.Add(‘ 風向:‘+Vartostr(ChildJson.Field[‘fx4‘].Value)+ ‘ ‘+Vartostr(ChildJson.Field[‘wind4‘].Value));
Memo1.Lines.Add(‘ 風力:‘+Vartostr(ChildJson.Field[‘wind4‘].Value));
Memo1.Lines.Add(FormatDateTime(‘YYYY年MM月DD日 ‘,DateUtils.IncDay(now,4))+‘:‘);
Memo1.Lines.Add(‘ 溫度:‘+Vartostr(ChildJson.Field[‘temp5‘].Value));
Memo1.Lines.Add(‘ 天氣:‘+Vartostr(ChildJson.Field[‘weather5‘].Value));
//Memo1.Lines.Add(‘ 風向:‘+Vartostr(ChildJson.Field[‘fx5‘].Value)+ ‘ ‘+Vartostr(ChildJson.Field[‘wind5‘].Value));
Memo1.Lines.Add(‘ 風力:‘+Vartostr(ChildJson.Field[‘wind5‘].Value));
end;
end;
procedure TForm4.FormCreate(Sender: TObject);
var
temp,str_1,str_2:string;
http1:tidhttp;
i:Integer;
begin
http1:=tidhttp.create(self);
temp:=HTTP1.Get(‘http://www.ipseeker.cn‘);
http1.free;
//temp:=Mmo1.Text;
i:=Pos(‘查詢結果‘,temp);
str_1:=Copy(temp,i,254);
str_2:=pro_result(str_1);
//Mmo2.Text:=str_2;
Caption := Trim(str_2);
ComboBox1.LoadFromRes(‘CityCode.Data‘);
i := Pos(‘ ‘,Caption);
if i <> 0 then
temp := copy(Caption,i+1,MaxInt)
else temp := Caption;
i := Pos(‘市‘,temp);
if i <> 0 then
temp := copy(temp,0,i-1);
ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(temp);
HttpReq := CoXMLHTTPRequest.Create;
Button1.Click;
end;
function TForm4.pro_result(str: string): string;
var
i_0,i_1:Integer;
temp_result:string;
begin
temp_result:=‘‘;
while Pos(‘查詢結果‘,str)<>0 do
begin
i_1:=Pos(‘</span>‘,str);
i_0:=Pos(‘查詢結果‘,str);
temp_result:=temp_result+Copy(str,i_0,i_1-i_0)+#13#10;
Delete(str,1,Pos(‘</span>‘,str));
end;
result:=StringReplace(temp_result,‘ ‘,‘‘,[rfReplaceAll]);
i_1 := Pos(‘-‘,Result);
Result := copy(Result,i_1+1,MaxInt);
end;
{ TComboBox }
constructor TComboBox.Create(AOwner: TComponent);
begin
inherited;
Values := TStringList.Create;
end;
destructor TComboBox.Destroy;
begin
Values.Free;
inherited;
end;
procedure TComboBox.LoadFromFile(Filename: string);
var
i: Integer;
begin
Items.Clear;
Values.LoadFromFile(Filename);
for i := 0 to Values.Count - 1 do
begin
Items.Add(Values.Names[i]);
end;
ItemIndex := 0;
end;
procedure TComboBox.LoadFromRes(ResName: string);
var
stream: TResourceStream;
i: Integer;
begin
stream := TResourceStream.Create(HInstance,‘CityCode‘,‘TxtData‘);
Items.Clear;
Values.LoadFromStream(stream);
for i := 0 to Values.Count - 1 do
begin
Items.Add(Values.Names[i]);
end;
ItemIndex := 0;
stream.Free;
end;
end.