类的示例demo
请求一个url,可以设置是否请求天气,广告,信息流的信息(url中有相应的参数字段),返回结果是json。
下面代码是定义了一个类CQueryServer,构造函数有三个参数,分别对应上面的信息。 原谅我把url改成了无效的
类定义如下:
class CQuerySvr
url="http://xxx.xxx.xxx.xxx/popwnd/newa?&m2=" + js.mid.m2
func __init__(self, query_black, query_weather, ad_type)
self.url = CQuerySvr.url
self.black_flag=query_black
self.weather_flag=query_weather
if(ad_type == 0)
then
self.ad_flag=0
self.url = js.string.combine(self.url, "&need_ad=0")
else
self.ad_flag=1
self.url = js.string.combine(self.url, "&ad_type=", ad_type, "&need_ad=1")
end
if(query_black)
then
self.url = js.string.combine(self.url, "&need_blacklist=1")
else
self.url = js.string.combine(self.url, "&need_blacklist=0")
end
if(query_weather)
then
self.url = js.string.combine(self.url, "&need_weather=1")
else
self.url = js.string.combine(self.url, "&need_weather=0")
end
end
func __del__(self)
//析构函数必须有
end
func Query(self)
local stream = js.net.get_to_buffer(self.url)
local jsonvalue = js.json.parse(stream.asString())
if(js.debug.type("jsonvalue") == "empty")
then
return 0
end
if(jsonvalue.exist("data"))
then
self.data = jsonvalue.data
return 1
end
return 0
end
func is_pop(self)
if(self.exist("data"))
then
if(self.data.exist("is_pop"))
then
return self.data.is_pop
end
end
return 0
end
func GetTodayWeather(self)
if(self.exist("data"))
then
if(self.data.exist("weather"))
then
local now_weather = self.data.weather
return js.string.utf8_to_ascii(now_weather.today_weather), now_weather.today_tp
end
end
return "", 0
end
func GetTomorrowWeather(self)
if(self.exist("data"))
then
if(self.data.exist("weather"))
then
local now_weather = self.data.weather
return js.string.utf8_to_ascii(now_weather.tomorrow_weather), now_weather.tomorrow_tp
end
end
return "", 0
end
func GetAdInfo(self, ad)
if(self.exist("data"))
then
if(self.data.exist("ads"))
then
local ads_nodes = self.data.ads
if(ads_nodes.size() > 0)
then
local node = self.data.ads[0]
if(node.size() > 0)
then
ad.title = js.string.utf8_to_ascii(node.title)
ad.url = node.url
local images_url = node.images_url
if(js.debug.type("images_url") == "object")
then
ad.images_url = images_url[0]
end
if(node.exist("ext"))
then
local ext = node.ext
if(ext.size() > 0)
then
local clktk = ext.clktk
if(js.debug.type("clktk") == "object")
then
if(clktk.size() > 0)
then
ad.click_stat=""
foreach(item in clktk)
ad.click_stat = ad.click_stat + item + ","
end
end
end
local imptk = ext.imptk
if(js.debug.type("imptk") == "object")
then
if(imptk.size() > 0)
then
ad.show_stat=""
foreach(item in imptk)
ad.show_stat = ad.show_stat + item + ","
end
end
end
local clstk = ext.clstk
if(js.debug.type("clstk") == "object")
then
if(clstk.size() > 0)
then
ad.dislike_stat=""
foreach(item in clstk)
ad.dislike_stat = ad.dislike_stat + item + ","
end
end
end
end
end
end
end
end
end
end
endclass
调用方式如下:
local svr = CQuerySvr(1, 1, 1)
if(svr.Query())
then
if(svr.weather_flag)
then
local today_weather, today_tp = svr.GetTodayWeather()
local tm_weather, tm_tp = svr.GetTomorrowWeather()
js.ui.messagebox("设置了请求天气,请求的结果是:\r\n" + "今天天气\t" + today_weather + "\t气温:\t" + today_tp + "\r\n明天天气\t" + tm_weather + "\t气温:\t" + tm_tp, "是否显示天气?", 0)
end
if(svr.ad_flag)
then
js.ui.messagebox("设置了请求广告", "是否请求广告", 0)
_ad = {}
svr.GetAdInfo(_ad)
str = "新闻标题\t" + _ad.title + "\r\n新闻url\t" + _ad.url + "\r\n新闻图片\t" + _ad.images_url + "\r\n显示打点\t" + _ad.show_stat + "\r\n点击打点\t" + _ad.click_stat + "\r\n不喜欢打点\t" + _ad.dislike_stat
js.ui.messagebox(str, "广告详情", 0)
end
end