[Solved] err_cleartext_not_permitted

Building an App that connects to Open Hardware Monitor server [https://openhardwaremonitor.org/] (https://openhardwaremonitor.org/)
on local LAN http://192.168.68.115:8085/ which works 100% on phone with chrome browser.
Problem seems to be that the server is sending it via HTTP not S and the server cannot be changed to HTTPS either.
It runs fine on Solar2D, but not if compiled to apk and running on the phone.
There is related info on this error on the forum and they talk about a workaround, but does not give a clear solution.
build.settings:

-- build.settings
settings =
{
    orientation =
    {
        default = "portrait",
        supported = { "portrait" },
    },

    android =
    {
        usesPermissions =
        {
            "android.permission.INTERNET",
            "android.permission.ACCESS_NETWORK_STATE",
        },
        applicationChildElements =
        {
            [[
            <meta-data android:name="android.support.multidex.MultiDexApplication" android:value="true" />
            ]],
            [[
            <meta-data android:name="android:networkSecurityConfig" android:resource="@xml/network_security_config"/>
            ]]
        },
    },

    iphone =
    {
        xcassets = "Images.xcassets",
        plist =
        {
            UIStatusBarHidden = false,
            UILaunchStoryboardName = "LaunchScreen",
        },
    },

    plugins = {},

    excludeFiles =
    {
        all = { "Icon.png", "Icon-*dpi.png", "Images.xcassets" },
        android = { "LaunchScreen.storyboardc" },
    },
}

main.lua:

-- main.lua

-- Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

-- Function to handle platform-specific code
local function checkPlatformAndRequestPermissions()
    if system.getInfo("platform") == "android" then
        -- Permissions are declared in build.settings for Android
        print("Running on Android device. Permissions should be declared in build.settings.")
    else
        print("Running on a non-Android device. No special permissions needed.")
    end
end

-- Call the platform check function
checkPlatformAndRequestPermissions()

-- Optional: Add a background color for the web view
local background = display.newRect(display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight)
background:setFillColor(1, 1, 1)
background:toBack() -- Send the background behind the web view

-- Create a text field for URL input
local urlField = native.newTextField(display.contentCenterX, display.contentCenterY - 200, 300, 40)
urlField.placeholder = "Enter IP address (e.g., 192.168.68.115:8085)"

-- Debugging text
local debugText = display.newText("", display.contentCenterX, display.contentCenterY + 200, native.systemFont, 16)
debugText:setFillColor(1, 0, 0)

-- Create a web view variable
local webView

-- Function to load the URL in the web view
local function loadUrl()
    local ipAddress = urlField.text
    if ipAddress == "" then
        debugText.text = "Please enter a valid IP address."
        return
    end
    
    -- Check if the URL starts with http:// or https://
    if not ipAddress:match("^http://") and not ipAddress:match("^https://") then
        ipAddress = "http://" .. ipAddress
    end
    
    local url = ipAddress
    debugText.text = "Loading URL: " .. url
    
    if webView then
        webView:removeSelf()
        webView = nil
    end
    
    webView = native.newWebView(display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight)
    webView:request(url)
end

-- Create a button to load the URL
local loadButton = display.newText("Load URL", display.contentCenterX, display.contentCenterY - 140, native.systemFont, 24)
loadButton:setFillColor(0.2, 0.5, 1)
loadButton:addEventListener("touch", function(event)
    if (event.phase == "ended") then
        loadUrl()
    end
end)

-- Ensure cleanup of native objects on exit
local function onSystemEvent(event)
    if (event.type == "applicationExit") then
        if webView then
            webView:removeSelf()
            webView = nil
        end
    end
end

Runtime:addEventListener("system", onSystemEvent)

network_security_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.0.0/16</domain>
    </domain-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.0.0/8</domain>
    </domain-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">172.16.0.0/12</domain>
    </domain-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">*</domain>
    </domain-config>
</network-security-config>

Please assist?

I’m not sure if this will help but try to change the content of the xml file to the below only:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>
1 Like

That’s it!
I used

<domain-config cleartextTrafficPermitted="true">

And should have used

<base-config cleartextTrafficPermitted="true" />

Thank you very much @kakula

1 Like