Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Is there a minimum required Android version for network.request?
Started by sr_friend Jul 01 2018 06:08 AM

3 replies to this topic
network request android php android version

Best Answer sr_friend , 04 July 2018 - 12:24 PM

I resolved this issue. 

 

The URL was being sent malformed so needed the lua function as per link below to encode it all properly.

 

https://forums.coronalabs.com/topic/36511-networkrequest-invalid-parameter-url-argument-was-malformed-url/

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sr_friend

[GLOBAL: userInfoPane.html]
sr_friend
  • Observer

  • 4 posts
  • Corona SDK

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:

 

display.setStatusBar( display.HiddenStatusBar )
 
-- include the Corona "composer" module
local composer = require "composer"
 
-- load title screen
composer.gotoScene( "title", "fade" )
 
Main screen (called "title.lua") to submit native text input:

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
 
Screen that is called by network listener and which should show a response method ("successTest.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
 
 
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();
 

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,380 posts
  • Corona SDK

Hi.  It's great that you provided code, but attaching a micro project we can download and run will get more help.

 

Nobody here wants to type all this code in.  As well, I don't see your build.settings setup, which is the first thing that comes to mind when you say it doesn't work on your Android device.

 

You can attach a runable project( min of main.lua, config.lua, build.settings) as a zip file by

 

1. Click 'more reply options' below.

 

2. Then:

attaching_files.jpg



[TOPIC: post.html]
#3

sr_friend

[GLOBAL: userInfoPane.html]
sr_friend
  • Observer

  • 4 posts
  • Corona SDK

Hi,

 

Sorry. I did try to attach the lua files originally but it said that file type was not allowed (only the php one).

 

Here they are.

Attached Files



[TOPIC: post.html]
#4

sr_friend

[GLOBAL: userInfoPane.html]
sr_friend
  • Observer

  • 4 posts
  • Corona SDK

  Best Answer

I resolved this issue. 

 

The URL was being sent malformed so needed the lua function as per link below to encode it all properly.

 

https://forums.coronalabs.com/topic/36511-networkrequest-invalid-parameter-url-argument-was-malformed-url/


  • roaminggamer likes this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: network request, android, php, android version