【问题标题】:Corona SDK: network.request not working on AndroidCorona SDK:network.request 无法在 Android 上运行
【发布时间】:2013-07-03 17:28:40
【问题描述】:

此代码在 iOS 上完美运行。

当我在我的 SGSII Android 手机上运行相同的行时,根本没有任何进展。文本laddar... 只是无限循环。

我已经添加了

"android.permission.INTERNET",

到我的 build.settings 文件。

这是我的代码:

local function fetchDataFromURL()


    local loadingText = display.newText("no text", 0,0,"Sintony", 40)
    loadingText.text = "Laddar stipendier..."
    loadingText:setReferencePoint(display.CenterLeftReferencePoint)
    loadingText.x = mainBox.x - loadingText.width/2
    loadingText.y = mainBox.y



    local function updateLoadingText()
        if (loadingText.text == "Laddar stipendier") then loadingText.text = "Laddar stipendier."  
        elseif (loadingText.text == "Laddar stipendier.") then loadingText.text = "Laddar stipendier.."   
        elseif (loadingText.text == "Laddar stipendier..") then loadingText.text = "Laddar stipendier..." 
        elseif (loadingText.text == "Laddar stipendier...") then loadingText.text = "Laddar stipendier" 
        end 
        loadingText:setReferencePoint(display.CenterLeftReferencePoint)
        loadingText.x = 134
    end
    local loadingTimer = timer.performWithDelay(500, updateLoadingText, 0)

    local function compare(a,b)
        return a.title < b.title
    end



    local function infoFetch(event)
            -- perform basic error handling
            if ( event.isError ) then
                print( "Network error!")
            else
            local data = json.decode(event.response)
            scholarshipTable = data.scholarships

            for key, value in pairs(scholarshipTable) do
                table.insert(namesOfScholarship,{title = value.title, id = value.id})
            end
            table.sort(namesOfScholarship, compare)

            timer.cancel(loadingTimer)
            loadingText:removeSelf()
            loadingText = nil
            createTableView()
            return true
            end
        end

      network.request( dataURL, "GET", infoFetch )
end

我看不出这段代码有什么问题,而且我对它在我的安卓设备上不起作用这一事实感到困惑。

感谢任何帮助

【问题讨论】:

  • 如果 HTTP 请求返回状态为 201 时会触发一个错误,它似乎永远不会返回,但不久前已修复。使用的是什么版本的 Corona?您在哪个版本的 Android 上运行?
  • 有趣。我相信是 .1137 或 .1156
  • 这个bug出现在哪个版本?

标签: lua coronasdk corona-storyboard


【解决方案1】:

更新到最新版本解决了我的问题

【讨论】:

    【解决方案2】:

    我看不出你的代码有什么问题

    我认为问题是你的android设备,有时设备的互联网连接是问题

    您可以在请求中添加超时

    local params = {}
    params.timeout = 3 --in seconds
    
    network.request( dataURL, "GET", infoFetch , params)
    

    所以如果 3 秒内没有加载,它将返回 event.isError

    【讨论】:

    • 您是否检查了您的权限并尝试了代码?尝试在您的事件监听器上添加打印
    • 在模拟器上一切正常。来自 network.request 的回调永远不会被触发 .. :/
    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多