Network.request Doesn't Work inside Scene

Hi, I had a problem on call the network.request from the scene. It’s returned bad request when activate in scene. But it’s working when I place the code in main.lua. Any advice on this? network.request(“http://xxx.xxxx.com/test.asmx”, “POST”, networkListener, params) Thank you.

Is it because either the “params” or “networkListener” are out of scope when you make the request inside your scene?

I am using the same set of code in the scene. I placed network.request code in the enterscene and created networkListener function out side of enterscene function. Both as shown below. I tried to use the print_r function to list the request content and it’s returned with 400 Bad Request (as shown at the botton).

  local function networkListener( event )
   print(“networklistener…”)
  if ( event.isError ) then
     
   --myText.text = “Network error!”
   print ( “network error…”)   
  else
   xmlString= event.response   
   print ( "network XML String  : " … xmlString )   
            
  end
 end

function scene:enterScene( event )
    local group = self.view

   local xmlRequest = [[
   <?xml version=“1.0” encoding=“utf-8”?>
   <soap:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>
   <soap:Body>
   <download xmlns=“http://www.xxxx.com/”>   
   <ID>aaa</ID>
   <Password>pwd</Password>
   </download>
   </soap:Body>
   </soap:Envelope>
   ]]
   
   local header = {}
   header[“SOAPAction”] = “http://www.xxxx.com/download
   header[“Content-Type”] = “text/xml”
   
    local params1 = {}
   params1.headers = header
   params1.body = xmlRequest

   network.request(“http://xxx.xxxx.com/test.asmx”, “POST”, networkListener, params1)

end

print_r Result

  [responseHeaders] => table: 005D3AB8 {
                         [X-Powered-By] => “ASP.NET
                         [Content-Length] => “0”
                         [Cache-Control] => “private”
                         [Content-Type] => “text/xml; charset=utf-8”
                         [Date] => “Thu, 09 Jan 2014 02:22:00 GMT”
                         [X-AspNet-Version] => “4.0.30319”
                         [HTTP-STATUS-LINE] => “HTTP/1.1 400 Bad Request”
                         [Server] => “Microsoft-IIS/7.5”
                       }
  [responseType] => “text”
  [phase] => “ended”
  [bytesEstimated] => 0
  [response] => “”
  [name] => “networkRequest”
  [bytesTransferred] => 0
  [status] => 400
  [url] => “http://xxx.xxx.com/test.asmx
  [isError] => false
  [requestId] => userdata: 005D3C60

Hi! AlanPlantPot, thank you for your reply.

I had rectified the problem. It’s due to the leading spaces in my xmlRequest string line (as shown below). It’s will works once the leading spaces is removed. Just not to put any spaces or tab in front of these code <?xml version=“1.0” encoding=“utf-8”?>  . This work for me. Hope this will help others as well.

Wrong code.

local xmlRequest = [[
           <?xml version=“1.0” encoding=“utf-8”?>

Correct code.

local xmlRequest = [[<?xml version=“1.0” encoding=“utf-8”?>

or

local xmlRequest = [[
<?xml version=“1.0” encoding=“utf-8”?>

Is it because either the “params” or “networkListener” are out of scope when you make the request inside your scene?

I am using the same set of code in the scene. I placed network.request code in the enterscene and created networkListener function out side of enterscene function. Both as shown below. I tried to use the print_r function to list the request content and it’s returned with 400 Bad Request (as shown at the botton).

  local function networkListener( event )
   print(“networklistener…”)
  if ( event.isError ) then
     
   --myText.text = “Network error!”
   print ( “network error…”)   
  else
   xmlString= event.response   
   print ( "network XML String  : " … xmlString )   
            
  end
 end

function scene:enterScene( event )
    local group = self.view

   local xmlRequest = [[
   <?xml version=“1.0” encoding=“utf-8”?>
   <soap:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>
   <soap:Body>
   <download xmlns=“http://www.xxxx.com/”>   
   <ID>aaa</ID>
   <Password>pwd</Password>
   </download>
   </soap:Body>
   </soap:Envelope>
   ]]
   
   local header = {}
   header[“SOAPAction”] = “http://www.xxxx.com/download
   header[“Content-Type”] = “text/xml”
   
    local params1 = {}
   params1.headers = header
   params1.body = xmlRequest

   network.request(“http://xxx.xxxx.com/test.asmx”, “POST”, networkListener, params1)

end

print_r Result

  [responseHeaders] => table: 005D3AB8 {
                         [X-Powered-By] => “ASP.NET
                         [Content-Length] => “0”
                         [Cache-Control] => “private”
                         [Content-Type] => “text/xml; charset=utf-8”
                         [Date] => “Thu, 09 Jan 2014 02:22:00 GMT”
                         [X-AspNet-Version] => “4.0.30319”
                         [HTTP-STATUS-LINE] => “HTTP/1.1 400 Bad Request”
                         [Server] => “Microsoft-IIS/7.5”
                       }
  [responseType] => “text”
  [phase] => “ended”
  [bytesEstimated] => 0
  [response] => “”
  [name] => “networkRequest”
  [bytesTransferred] => 0
  [status] => 400
  [url] => “http://xxx.xxx.com/test.asmx
  [isError] => false
  [requestId] => userdata: 005D3C60

Hi! AlanPlantPot, thank you for your reply.

I had rectified the problem. It’s due to the leading spaces in my xmlRequest string line (as shown below). It’s will works once the leading spaces is removed. Just not to put any spaces or tab in front of these code <?xml version=“1.0” encoding=“utf-8”?>  . This work for me. Hope this will help others as well.

Wrong code.

local xmlRequest = [[
           <?xml version=“1.0” encoding=“utf-8”?>

Correct code.

local xmlRequest = [[<?xml version=“1.0” encoding=“utf-8”?>

or

local xmlRequest = [[
<?xml version=“1.0” encoding=“utf-8”?>