lae界面開發工具入門之介紹十三--<如何獲取數據?> 原

測試程序下載:http://pan.baidu.com/s/1dERKj2D

APP數據來源大致分三種:
1、線上數據
    服務器端返回的數據,一般採用用HTTP或者HTTPS、SOCKET連接方式與APP客戶交互,可以返回json格式或者二進制。  


local json = luaopen_cjson();

function HelperURLResponeFunc(urlresponse)	
	local urlrequest = urlresponse:getRequest();
	if(urlrequest.thread ~= nil) then
		--LXZMessageBox("urlrequest.thread:"..type(urlrequest.thread));
		coroutine.resume(urlrequest.thread, urlresponse);
	end	
end


local service_host = "http://120.55.84.103:8080/";--服務器地址
local function HelperPostGetJSON(url, data, fn)

	local co = coroutine.create(function (thread)	
		local urlrequest = _urlRequest:new();
		urlrequest.nRequestType = eRequestPost;
		urlrequest.func = "HelperURLResponeFunc";
		urlrequest.url = url.."?";
		for k,v in pairs(data) do
			urlrequest.url = urlrequest.url ..k.."="..v.."&";
		end	
			
		urlrequest.url = TrimRightChar(urlrequest.url, '&');
		urlrequest.url = TrimRightChar(urlrequest.url, '?');
		urlrequest.thread = thread;
		--LXZMessageBox(urlrequest.url);
		
		local curl = CLXZCurl:Instance();
		curl:send(urlrequest);	
		local urlresponse = coroutine.yield();
		if(fn~=nil) then
			fn(urlresponse);
		end		
	end);
	
	coroutine.resume(co, co);	
	
end

--從服務器獲取數據
local function OnServer(window, msg, sender)
	local root=HelperGetRoot();--獲得根節點
	local wnd=root:GetLXZWindow("id");--獲得id窗口
	
	--創建協程,在協程裏可以順序執行異步代碼
	HelperCoroutine(function(thread)
		HelperPostGetJSON(service_host.."getid", {}, function(urlresponse)--異步調用接口
			LXZMessageBox("urlresponse:getResponseData:"..urlresponse:getResponseData())
			local res = json.decode(urlresponse:getResponseData());
			if res then
				HelperSetWindowText(wnd, res.id);
			else
				HelperSetWindowText(wnd, "從服務器獲取失敗");
			end
			coroutine.resume(thread);
		end);		
		coroutine.yield();	
	end);

end

 
        
2、本地配置數據    
    可讀取key-value模式文本數據文件

--從配置文件讀取數據
local function OnCfg(window, msg, sender)
	local root=HelperGetRoot();--獲得根節點
	local wnd=root:GetLXZWindow("id");--獲得id窗口
	
	local cfg = ILXZCoreCfg:new_local();
	cfg:load(LXZAPIGetWritePath().."data.cfg");
	local id = cfg:GetInt("id");
	if id then
		HelperSetWindowText(wnd, id);
	else
		HelperSetWindowText(wnd, "配置文件錯誤");
	end
	
	
end


        
3、sqlite3數據庫
    集成有sqlite數據庫,可直接跟sqlite交互。


--數據庫相關
local function DBInitialize(dbname)
	LXZDoFile("sqlite3.lua");	--數據庫模塊

	local corecfg = ICGuiGetLXZCoreCfg();	
	local  dbfullpath = "";
	if(corecfg.IsEditTool==true) then --如果是lae工具中.
		dbfullpath = LXZAPIGetWritePath().."Edit"..dbname; --防止產生數據庫同名時無法同時打開。
	else
		dbfullpath = LXZAPIGetWritePath()..dbname;
	end

	--打開sqlite3
	local db = sqlite3.open(dbfullpath);		
	db:exec("drop table Test");		--刪除存在的表	
	
	sql = "create table Test(id INTEGER PRIMARY KEY autoincrement, name VARCHAR(32), userid INTERGER)";
	local d,err = db:exec(sql);	--創建Test表,id作爲主鍵,自動增加,name名字可變長.
	
	sql = "insert into Test(id,name,userid) values(100, \"我是個測試名字!\", 1111111)" --插入測試數據.
	local d,err = db:exec(sql);		
	return db;
end

--文件加載時初始化.
local db=DBInitialize("TestData.db");
	
--從數據庫讀取數據
local function OnSqlite(window, msg, sender)
	local root=HelperGetRoot();--獲得根節點
	local wnd=root:GetLXZWindow("id");--獲得id窗口
	
	local sql="select * from Test where id=100";
	local d,err = db:exec(sql);				
	local users =  HelperDBSelect(db, sql,  true);	
	if users and users[1] then
		HelperSetWindowText(wnd, users[1].userid);
	else
		HelperSetWindowText(wnd, "該數據不存在.");
	end	
end

1、測試程序界面

測試代碼:

LXZDoFile("LXZHelper.lua");
LXZDoFile("serial.lua");
local json = luaopen_cjson();

function HelperURLResponeFunc(urlresponse)	
	local urlrequest = urlresponse:getRequest();
	if(urlrequest.thread ~= nil) then
		--LXZMessageBox("urlrequest.thread:"..type(urlrequest.thread));
		coroutine.resume(urlrequest.thread, urlresponse);
	end	
end


local service_host = "http://120.55.84.103:8080/";--服務器地址
local function HelperPostGetJSON(url, data, fn)

	local co = coroutine.create(function (thread)	
		local urlrequest = _urlRequest:new();
		urlrequest.nRequestType = eRequestPost;
		urlrequest.func = "HelperURLResponeFunc";
		urlrequest.url = url.."?";
		for k,v in pairs(data) do
			urlrequest.url = urlrequest.url ..k.."="..v.."&";
		end	
			
		urlrequest.url = TrimRightChar(urlrequest.url, '&');
		urlrequest.url = TrimRightChar(urlrequest.url, '?');
		urlrequest.thread = thread;
		--LXZMessageBox(urlrequest.url);
		
		local curl = CLXZCurl:Instance();
		curl:send(urlrequest);	
		local urlresponse = coroutine.yield();
		if(fn~=nil) then
			fn(urlresponse);
		end		
	end);
	
	coroutine.resume(co, co);	
	
end

--從服務器獲取數據
local function OnServer(window, msg, sender)
	local root=HelperGetRoot();--獲得根節點
	local wnd=root:GetLXZWindow("id");--獲得id窗口
	
	--創建協程,在協程裏可以順序執行異步代碼
	HelperCoroutine(function(thread)
		HelperPostGetJSON(service_host.."getid", {}, function(urlresponse)--異步調用接口
			LXZMessageBox("urlresponse:getResponseData:"..urlresponse:getResponseData())
			local res = json.decode(urlresponse:getResponseData());
			if res then
				HelperSetWindowText(wnd, res.id);
			else
				HelperSetWindowText(wnd, "從服務器獲取失敗");
			end
			coroutine.resume(thread);
		end);		
		coroutine.yield();	
	end);

end

--數據庫相關
local function DBInitialize(dbname)
	LXZDoFile("sqlite3.lua");	--數據庫模塊

	local corecfg = ICGuiGetLXZCoreCfg();	
	local  dbfullpath = "";
	if(corecfg.IsEditTool==true) then --如果是lae工具中.
		dbfullpath = LXZAPIGetWritePath().."Edit"..dbname; --防止產生數據庫同名時無法同時打開。
	else
		dbfullpath = LXZAPIGetWritePath()..dbname;
	end

	--打開sqlite3
	local db = sqlite3.open(dbfullpath);		
	db:exec("drop table Test");		--刪除存在的表	
	
	sql = "create table Test(id INTEGER PRIMARY KEY autoincrement, name VARCHAR(32), userid INTERGER)";
	local d,err = db:exec(sql);	--創建Test表,id作爲主鍵,自動增加,name名字可變長.
	
	sql = "insert into Test(id,name,userid) values(100, \"我是個測試名字!\", 1111111)" --插入測試數據.
	local d,err = db:exec(sql);		
	return db;
end

--文件加載時初始化.
local db=DBInitialize("TestData.db");
	
--從數據庫讀取數據
local function OnSqlite(window, msg, sender)
	local root=HelperGetRoot();--獲得根節點
	local wnd=root:GetLXZWindow("id");--獲得id窗口
	
	local sql="select * from Test where id=100";
	local d,err = db:exec(sql);				
	local users =  HelperDBSelect(db, sql,  true);	
	if users and users[1] then
		HelperSetWindowText(wnd, users[1].userid);
	else
		HelperSetWindowText(wnd, "該數據不存在.");
	end	
end

--從配置文件讀取數據
local function OnCfg(window, msg, sender)
	local root=HelperGetRoot();--獲得根節點
	local wnd=root:GetLXZWindow("id");--獲得id窗口
	
	local cfg = ILXZCoreCfg:new_local();
	cfg:load(LXZAPIGetWritePath().."data.cfg");
	local id = cfg:GetInt("id");
	if id then
		HelperSetWindowText(wnd, id);
	else
		HelperSetWindowText(wnd, "配置文件錯誤");
	end
	
	
end

--事件綁定
local event_callback = {}
event_callback.__index = event_callback;
event_callback ["OnServer"] = OnServer;
event_callback ["OnSqlite"] = OnSqlite;
event_callback ["OnCfg"] = OnCfg;

--消息派發接口
function main_dispacher(window, cmd, msg, sender)
	LXZAPI_OutputDebugStr("cmd 1:"..cmd);
	if(event_callback[cmd] ~= nil) then --如果已經綁定
		LXZAPI_OutputDebugStr("cmd 2:"..cmd);
		event_callback[cmd](window, msg, sender);--則調用綁定的接口
	end
end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章