I initially posted this is “Game network” and feel this may be a more appropriate place to post. I will delete the duplicate when it is posted.
I’ve been trying to solve this problem for a few weeks now and am giving up and posting here.
Is there a minimum required Android version for network.request? If so, please advise, if not, please read on.
I have a simple app that sends some data to a php file on my website, this then returns a response.
This process works fine in the simulator and on my actual S6 handset. I then tested it on my old Nexus 7 (running Android 5.1) and while the other parts of the app work, I cannot get a success message sent back by the php script. I say “success” as if I send it a bad network.request with no data the php script will send back a “failed” message. I also know it is not succeeding as the php script logs the request data and device id to a file but nothing is ever written from the Nexus 7 but is fine for the Simulator and S6.
I added a test so that I also know the Nexus 7 can access the internet (Following the steps from here: https://forums.coronalabs.com/topic/33356-check-for-internet-connection/ ).
I’ve tried slowing the request down (As per instructions here: https://forums.coronalabs.com/topic/66559-problems-with-networkrequest/ ) but to no avail.
I have stripped out all unrelated content and knocked up a dummy test app and barebones php script to show you my problem. Any help would be very much appreciated.
Test source code to follow:
Main.lua is pretty standard:
[lua]display.setStatusBar( display.HiddenStatusBar )
– include the Corona “composer” module
local composer = require “composer”
– load title screen
composer.gotoScene( “title”, “fade” )[/lua]
Main screen (called “title.lua”) to submit native text input:
[lua]
local json = require (“json”)
local composer = require( “composer” )
local scene = composer.newScene()
local defaultBox
local freeDesc = “Your message here”
local responses = “”
local freeDescEdit = “”
– start network listener
local function networkListener( event )
if ( event.isError ) then
responses = "Network error: " … event.response
local options = {
effect = “crossFade”,
time = 400,
params = { Status = responses }
}
timer.performWithDelay( 10, function()
composer.gotoScene( “successTest”, options )
end, 1)
else
responses = event.response
if (responses == ‘\nSubmitted Successfully’) then
local options = {
effect = “crossFade”,
time = 400,
params = { Status = responses }
}
timer.performWithDelay( 10, function()
composer.gotoScene( “successTest”, options )
end, 1)
– Check not blank thus no message to show
elseif (string.len(responses) > 1) then
local options = {
effect = “crossFade”,
time = 400,
params = { Status = responses }
}
timer.performWithDelay( 10, function()
composer.gotoScene( “successTest”, options )
end, 1)
else
local options = {
effect = “crossFade”,
time = 400,
params = { Status = “Received blank message” }
}
timer.performWithDelay( 10, function()
composer.gotoScene( “successTest”, options )
end, 1)
end
end
end
– end network listener
local function sendSubmission()
native.setKeyboardFocus( nil )
freeDesc = freeDescEdit
– Send network request to php script on server
– Good test - Should succeed. This request does not show anything on Nexus 7
network.request( “http://www.wolfshield.co.uk/testing/test.php?freeDesc=” … freeDesc, “GET”, networkListener )
– Bad test - Should fail. This shows a “Submision failed” error on Nexus 7 (as expected)
--network.request( “http://www.wolfshield.co.uk/testing/test.php”, “GET”, networkListener )
– End network request
end
local function boxListener( event )
if ( event.phase == “began” ) then
elseif ( event.phase == “ended” or event.phase == “submitted” ) then
freeDesc = event.target.text
elseif ( event.phase == “editing” ) then
freeDescEdit = event.text
end
end
function scene:create( event )
local sceneGroup = self.view
local submitButton = display.newText( sceneGroup, “Send to Wolfshield.co.uk”, display.contentCenterX, display.contentHeight * 0.73, mainFont, 60)
submitButton:setFillColor( 0, 0.4, 0.4 )
submitButton:addEventListener( “tap”, sendSubmission )
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
defaultBox = native.newTextBox( display.contentWidth * 0.49, display.contentHeight * 0.225, display.contentWidth * 0.95, display.contentHeight * 0.1 )
defaultBox.size = 32
defaultBox.text = freeDesc
defaultBox.isEditable = true
defaultBox:addEventListener( “userInput”, boxListener )
sceneGroup:insert( defaultBox )
elseif phase == “did” then
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
elseif phase == “did” then
defaultBox:removeSelf()
defaultBox = nil
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
[/lua]
Screen that is called by network listener and which should show a response method (“successTest.lua”):
[lua]
local composer = require( “composer” )
local scene = composer.newScene()
local responseX = " "
local function goBack()
composer.removeScene(“successTest”)
composer.gotoScene( “title”, “crossFade” )
end
function scene:create( event )
local sceneGroup = self.view
local responseX = event.params.Status
local options2 = {
text = responseX,
x = display.contentWidth * 0.1,
y = display.contentHeight * 0.5,
width = display.contentWidth * 0.9,
height = 0,
font = mainFont,
fontSize = 72,
align = “left”
}
local textField = display.newText( options2 )
textField:setFillColor( 1, 1, 1 )
textField.anchorX = 0
sceneGroup:insert( textField )
local gameSettingsButton = display.newText( sceneGroup, “Back”, display.contentCenterX, display.contentHeight * 0.8, mainFont, 72)
gameSettingsButton:setFillColor( 0.95, 0.2, 0.2 )
gameSettingsButton:addEventListener( “tap”, goBack )
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
elseif phase == “did” then
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
elseif phase == “did” then
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
[/lua]
Finally simple php script for testing:
<?php
function sendResponse($status = 200, $body = ’ ', $content_type = ‘text/html’)
{
http_response_code($status);
header('Content-type: ’ . $content_type);
echo $body;
}
class ReceiveAPI {
private $db;
function receive() {
// Check for required parameters
if (isset($_GET[“freeDesc”])) {
// Put parameters into local variables
$freeDesc = $_GET[“freeDesc”];
sendResponse(200, ‘Submitted Successfully’);
return true;
}
sendResponse(400, ’ Submission Failed ');
return false;
}
}
$api = new ReceiveAPI;
$api->receive();