﻿{"id":23,"date":"2014-08-10T10:27:28","date_gmt":"2014-08-10T02:27:28","guid":{"rendered":"http:\/\/www.tansoo.cn\/?p=23"},"modified":"2014-09-10T10:32:23","modified_gmt":"2014-09-10T02:32:23","slug":"%e5%88%a9%e7%94%a8%e7%99%be%e5%ba%a6%e5%9c%b0%e5%9b%beapi%e5%be%97%e5%88%b0%e6%8c%87%e5%ae%9a%e5%9f%8e%e5%b8%82%e7%9a%84%e5%a4%a9%e6%b0%94%e9%a2%84%e6%8a%a5","status":"publish","type":"post","link":"http:\/\/www.tansoo.cn\/?p=23","title":{"rendered":"\u5229\u7528\u767e\u5ea6\u5730\u56feAPI\u5f97\u5230\u6307\u5b9a\u57ce\u5e02\u7684\u5929\u6c14\u9884\u62a5"},"content":{"rendered":"<p>\u5229\u7528\u767e\u5ea6\u5730\u56fe\u8f66\u8054\u7f51API\uff0c\u6839\u636e\u57ce\u5e02\u540d\u67e5\u8be2\u5929\u6c14\u7684\u7ed3\u679c\uff0c\u5e76\u5c06\u5929\u6c14\u56fe\u7247\u4e0b\u8f7d\u5230\u6307\u5b9a\u76ee\u5f55\uff0c\u5229\u4e8e\u540e\u671f\u663e\u793a\u3002\u6e90\u4ee3\u7801\u4e2d\u8981\u7528\u5230\u7684\u4e00\u4e9b\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7ed3\u6784\u8bf7\u5728\u672c\u7ad9\u5176\u4ed6\u4ee3\u7801\u4e2d\u67e5\u627e\u3002 <!--more--><\/p>\n<pre class=\"lang:delphi decode:true \" >function TfrmMain.GetWeather(City: string): WeatherResults;\r\nvar\r\n  i, J, P, count: Integer;\r\n  Url, PicPath: string;\r\n  node: IXMLNode;\r\n  XMLDoc: TXMLDocument;\r\n  nodeList: IXMLNodeList;\r\n  tStream: TFileStream;\r\n  IdHTTP: TIdHTTP;\r\nbegin\r\n  XMLDoc := TXMLDocument.Create(Self);\r\n  IdHTTP := TIdHTTP.Create;\r\n  try\r\n    Url := UrlEncode(Format(\r\n        'http:\/\/api.map.baidu.com\/telematics\/v3\/weather?location=%s&amp;output=xml&amp;coord_type=wgs84&amp;ak=A4c132a2db250e33cd1e2af37076ac28', [City]));\r\n    try\r\n      XMLDoc.XML.Text := IdHTTP.Get(Url);\r\n      XMLDoc.Active := True;\r\n      node := XMLDoc.DocumentElement.ChildNodes[3]; \/\/ &lt;results&gt;\r\n\r\n      Result.error := XMLDoc.DocumentElement.ChildValues['error'];\r\n      Result.status := XMLDoc.DocumentElement.ChildValues['status'];\r\n      Result.date := XMLDoc.DocumentElement.ChildValues['date'];\r\n      Result.currentCity := node.ChildValues['currentCity'];\r\n      Result.pm25 := node.ChildValues['pm25'];\r\n\r\n      nodeList := node.ChildNodes[1].ChildNodes; \/\/ &lt;weather_data&gt;\r\n\r\n      count := nodeList.count;\r\n\r\n      i := 0;\r\n      for J := 1 to 4 do\r\n      begin\r\n        Result.Data[J].date := string(nodeList[i].NodeValue);\r\n        Inc(i);\r\n        Result.Data[J].dayPictureUrl := string(nodeList[i].NodeValue);\r\n        Inc(i);\r\n        Result.Data[J].nightPictureUrl := string(nodeList[i].NodeValue);\r\n        Inc(i);\r\n        Result.Data[J].weather := string(nodeList[i].NodeValue);\r\n        Inc(i);\r\n        Result.Data[J].wind := string(nodeList[i].NodeValue);\r\n        Inc(i);\r\n        Result.Data[J].temperature := string(nodeList[i].NodeValue);\r\n        Inc(i);\r\n\r\n        P := Pos('\/images\/', Result.Data[J].dayPictureUrl);\r\n        PicPath := ExePath + copy(Result.Data[J].dayPictureUrl, P + 1,\r\n          Length(Result.Data[J].dayPictureUrl) - P);\r\n        PicPath := ReplaceChar(PicPath, '\/', '\\');\r\n        if not FileExists(PicPath) then\r\n        begin\r\n          tStream := TFileStream.Create(PicPath, fmCreate);\r\n          try\r\n            IdHTTP.Get(Result.Data[J].dayPictureUrl, tStream); \/\/ \u958b\u59cb\u4e0b\u8f09\r\n          finally\r\n            tStream.Free;\r\n          end;\r\n        end;\r\n\r\n        P := Pos('\/images\/', Result.Data[J].nightPictureUrl);\r\n        PicPath := ExePath + copy(Result.Data[J].nightPictureUrl, P + 1,\r\n          Length(Result.Data[J].nightPictureUrl) - P);\r\n        PicPath := ReplaceChar(PicPath, '\/', '\\');\r\n        if not FileExists(PicPath) then\r\n        begin\r\n          tStream := TFileStream.Create(PicPath, fmCreate);\r\n          try\r\n            IdHTTP.Get(Result.Data[J].nightPictureUrl, tStream); \/\/ \u958b\u59cb\u4e0b\u8f09\r\n          finally\r\n            tStream.Free;\r\n          end;\r\n        end;\r\n      end;\r\n    except\r\n      abort;\r\n    end;\r\n  finally\r\n    XMLDoc.Free;\r\n    IdHTTP.Free;\r\n  end;\r\nend;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528\u767e\u5ea6\u5730\u56fe\u8f66\u8054\u7f51API\uff0c\u6839\u636e\u57ce\u5e02\u540d\u67e5\u8be2\u5929\u6c14\u7684\u7ed3\u679c\uff0c\u5e76\u5c06\u5929\u6c14\u56fe\u7247\u4e0b\u8f7d\u5230\u6307\u5b9a\u76ee\u5f55\uff0c\u5229\u4e8e\u540e\u671f\u663e\u793a\u3002\u6e90\u4ee3\u7801\u4e2d\u8981\u7528\u5230\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[6,5,9],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-program","tag-6","tag-5","tag-api"],"_links":{"self":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23"}],"version-history":[{"count":0,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}