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、测试程序界面

103855_sjZ3_1030910.png

测试代码:

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

 

转载于:https://my.oschina.net/u/1030910/blog/725493

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值