Qt 天氣預報

2021-10-24 07:16:21 字數 2455 閱讀 6845

通過呼叫天氣伺服器介面來獲取天氣資訊,隨後對獲取到的json格式資訊進行提取和轉換

這裡給出乙個獲取天氣伺服器的介面:

執行結果如下:

獲取資訊功能實現:

weather.cpp:

#include

"weather.h"

weather::

weather

(qobject *parent)

:qobject

(parent)

weather::

weather

(qstring cityname)

void weather::

refresh()

void weather::

queryweather()

//json檔案 例:合肥

//**sondocument(,

//

// ,,

// ,

// ],

// "ganmao":"感冒低發期,天氣舒適,請注意多吃蔬菜水果,多喝水哦。","wendu":"25","yesterday":},"desc":"ok","status":1000})

qmap

> weather::

getdatamap

(bool

*ok)if(

!this

->datamap.

isempty()

)*ok=

false;if

(!this

->isgetdata)

//是否成功獲取資料

return

this

->datamap;

**sonparseerror err;

**sondocument json_recv=**sondocument::

fromjson

(allinfo.

toutf8()

,&err)

;//解析json物件if(

!json_recv.

isnull()

) value=object_data.

value

("forecast");

if(value.

isarray()

)*ok=

true

;return datamap;}}

}}}return datamap;

}void weather::

replyfinished

(qnetworkreply *reply)

//重新整理的槽}}

}}reply-

>

deletelater()

;if(isgetdata)

else

emit this

->

getdatafinisedsignal()

;}qstring weather::

getallinfo()

void weather::

setcityname

(qstring city)

qstring weather::

getcityname()

qstring weather::

getdate()

qstring weather::

gettemperature()

qstring weather::

gettemperaturemax()

qstring weather::

getwind()

qstring weather::

getfluenza()

qstring weather::

getweathertype()

weather.h:
#ifndef weather_h

#define weather_h

#include

#include

#include

#include

#include

#include

#include

class

weather

:public qobject

;#endif

// weather_h

原始碼:

Flex 天氣預報

效果如下截圖 建立乙個flex專案weather 在weather.mxml中新增乙個httpwebservice如下 result onresult event resultformat e4x 定義乙個onresult函式如下 bindable private var xml xml new x...

python天氣預報

1.引入requests import requests def query now 查詢實時天氣 return none query city input 請輸入要查詢的城市名稱 2.準備url位址 url query city 3.發請求,接收資料 response requests.get u...

天氣預報小偷

首先,進行小偷程式的一些初始化設定,以上 的作用分別是忽略掉所有非致命性錯誤,把小偷程式的執行超時時間設定得很長 這樣不會出現執行超時的錯誤 轉換原來預設的utf 8編碼轉換成gb2312編碼,否則直接用xmlhttp元件呼叫有中文字元的網頁得到的將是亂碼。然後呼叫xmlhttp元件建立乙個物件並進...