--获取ip
function Tools:getIp( )
local xhr = cc.XMLHttpRequest:new()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
xhr:open("GET", "http://pv.sohu.com/cityjson?ie=utf-8")
local function onReadyStateChanged()
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
local response= xhr.response
local startPos = string.find(response,"{")
local endPos = string.find(response,"}")
local jsonStr = string.sub(response,startPos,endPos)
local tb = json.decode(jsonStr)
--获取的ip地址
local ip = tb.cip
--获取归属地
local cname = tb.cname
self:log("ip ".. ip .. " 归属地 " .. cname)
return ip
else
return "0.0.0.0"
end
xhr:unregisterScriptHandler()
end
xhr:registerScriptHandler(onReadyStateChanged)
xhr:send()
end