Android 4.3 network.request and POST

My customers running Android 4.3 are having a whole bunch of issues with network connections.  I saw a couple threads about it, and it was recommended that you retry network requests because Android 4.3 is failing often, but when I tried that I was getting even more bizarre behavior.  

It seems like if you call network.request with a POST it isn’t passing the parameters.  So instead of getting a failure in the callback.  I am getting a response from my php server saying the parameters are undefined indexes.  

l attached a sphp server file for a test app that demonstrates the issues if you run it on Android 4.3.  If you only click on the Get Test Sets button everything works just fine.  If you click on the Post Test Sets then some requests work fine, but others seem to lose the parameters, and the ones that do work come back out of order (which I know order isn’t guaranteed but it is still weird) and if you go back to the Get Test Sets, those lose parameters as well.  

local widget = require "widget" function network.requestWithRetry(url, postOrGet, networkListener, params)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;print("URL " .. url) &nbsp;&nbsp;&nbsp;&nbsp;if(params ~= nil) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("PARAMS " .. params.body) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local count = 0 &nbsp;&nbsp;&nbsp;&nbsp;local function localListener( event ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("in retry listener") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(event.phase) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(event.phase == "ended") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( event.isError ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(count \< 5) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count = count + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("try " .. tostring(count)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doRequest() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Network error " .. tostring(event.errorMessage)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("response " .. event.response) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;networkListener(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local function doRequest() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(postOrGet == "GET") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;network.request(url, postOrGet, localListener)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;network.request(url, postOrGet, localListener, params)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;doRequest() &nbsp;&nbsp;&nbsp;&nbsp; end local function handler(event) &nbsp;&nbsp;&nbsp;&nbsp;print("IN REAL HANDLER") &nbsp;&nbsp;&nbsp;&nbsp;print("IS ERROR " .. tostring(event.isError)) &nbsp;&nbsp;&nbsp;&nbsp;print("RESPONSE " .. tostring(event.response)) &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; end local function testSet1() &nbsp;&nbsp;&nbsp;&nbsp;postData = "test1=yes&test2=no" &nbsp;&nbsp;&nbsp;&nbsp;local params = {} &nbsp;&nbsp;&nbsp;&nbsp;params.body = postData &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;print("POST TEST") &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php", "POST", handler, params) &nbsp;&nbsp;&nbsp;&nbsp;print("POST TEST2") &nbsp;&nbsp;&nbsp;&nbsp;params.body = "test1=boo&test2=hoo" &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php", "POST", handler, params) &nbsp;&nbsp;&nbsp;&nbsp;print("POST TEST3") &nbsp;&nbsp;&nbsp;&nbsp;params.body = "test1=high&test2=low" &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php", "POST", handler, params) &nbsp;&nbsp;&nbsp;&nbsp;print("GET TEST") &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php?test1=red&test2=green", "GET", handler, nil) end local function testSet2() &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;print("GET TEST") &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php?test1=yes&test2=no", "GET", handler, nil) &nbsp;&nbsp;&nbsp;&nbsp;print("GET TEST2") &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php?test1=boo&test2=hoo", "GET", handler, nil) &nbsp;&nbsp;&nbsp;&nbsp;print("GET TEST3") &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php?test1=high&test2=low", "GET", handler, nil) &nbsp;&nbsp;&nbsp;&nbsp;print("GET TEST") &nbsp;&nbsp;&nbsp;&nbsp;network.requestWithRetry("http://192.168.1.6:5723/testNetwork.php?test1=red&test2=green", "GET", handler, nil) end local button1 = widget.newButton{ &nbsp;&nbsp;&nbsp;&nbsp;label = "Post Test Sets", &nbsp;&nbsp;&nbsp;&nbsp;onPress = function(event)testSet1() return true end } button1.y = display.contentHeight \* .3 button1.x = display.contentWidth \* .5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local button2 = widget.newButton{ &nbsp;&nbsp;&nbsp;&nbsp;label = "Get Test Sets", &nbsp;&nbsp;&nbsp;&nbsp;onPress = function(event)testSet2() return true end } button2.y = display.contentHeight \* .6 button2.x = display.contentWidth \* .5 &nbsp;

Here is the output from logcat:

Running just the Get Test Sets Button:

12-11 17:58:01.589: I/Corona(11656): GET TEST

12-11 17:58:01.589: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=yes&test2=no

12-11 17:58:01.609: I/Corona(11656): GET TEST2

12-11 17:58:01.609: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=boo&test2=hoo

12-11 17:58:01.639: I/Corona(11656): GET TEST3

12-11 17:58:01.639: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=high&test2=low

12-11 17:58:01.681: I/Corona(11656): GET TEST

12-11 17:58:01.681: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=red&test2=green

12-11 17:58:02.089: I/Corona(11656): in retry listener

12-11 17:58:02.089: I/Corona(11656): ended

12-11 17:58:02.089: I/Corona(11656): response boo hoo

12-11 17:58:02.089: I/Corona(11656): IN REAL HANDLER

12-11 17:58:02.089: I/Corona(11656): IS ERROR false

12-11 17:58:02.089: I/Corona(11656): RESPONSE boo hoo

12-11 17:58:02.109: I/Corona(11656): in retry listener

12-11 17:58:02.109: I/Corona(11656): ended

12-11 17:58:02.109: I/Corona(11656): response high low

12-11 17:58:02.109: I/Corona(11656): IN REAL HANDLER

12-11 17:58:02.109: I/Corona(11656): IS ERROR false

12-11 17:58:02.109: I/Corona(11656): RESPONSE high low

12-11 17:58:02.129: I/Corona(11656): in retry listener

12-11 17:58:02.129: I/Corona(11656): ended

12-11 17:58:02.129: I/Corona(11656): response yes no

12-11 17:58:02.129: I/Corona(11656): IN REAL HANDLER

12-11 17:58:02.129: I/Corona(11656): IS ERROR false

12-11 17:58:02.129: I/Corona(11656): RESPONSE yes no

12-11 17:58:02.149: I/Corona(11656): in retry listener

12-11 17:58:02.149: I/Corona(11656): ended

12-11 17:58:02.149: I/Corona(11656): response red green

12-11 17:58:02.149: I/Corona(11656): IN REAL HANDLER

12-11 17:58:02.149: I/Corona(11656): IS ERROR false

12-11 17:58:02.149: I/Corona(11656): RESPONSE red green

Running the Post Test Sets

12-11 17:58:04.598: I/Corona(11656): POST TEST

12-11 17:58:04.598: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php

12-11 17:58:04.598: I/Corona(11656): PARAMS test1=yes&test2=no

12-11 17:58:04.608: I/Corona(11656): POST TEST2

12-11 17:58:04.608: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php

12-11 17:58:04.618: I/Corona(11656): PARAMS test1=boo&test2=hoo

12-11 17:58:04.648: I/Corona(11656): POST TEST3

12-11 17:58:04.648: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php

12-11 17:58:04.648: I/Corona(11656): PARAMS test1=high&test2=low

12-11 17:58:04.668: I/Corona(11656): GET TEST

12-11 17:58:04.679: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=red&test2=green

12-11 17:58:04.758: I/Corona(11656): in retry listener

12-11 17:58:04.758: I/Corona(11656): ended

12-11 17:58:04.768: I/Corona(11656): response yes no

12-11 17:58:04.788: I/Corona(11656): IN REAL HANDLER

12-11 17:58:04.788: I/Corona(11656): IS ERROR false

12-11 17:58:04.788: I/Corona(11656): RESPONSE yes no

12-11 17:58:04.868: I/Corona(11656): in retry listener

12-11 17:58:04.868: I/Corona(11656): ended

12-11 17:58:04.878: I/Corona(11656): response 

12-11 17:58:04.878: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:04.878: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:04.878: I/Corona(11656):  

12-11 17:58:04.878: I/Corona(11656): IN REAL HANDLER

12-11 17:58:04.878: I/Corona(11656): IS ERROR false

12-11 17:58:04.878: I/Corona(11656): RESPONSE 

12-11 17:58:04.878: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:04.878: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:04.878: I/Corona(11656):  

12-11 17:58:04.898: I/Corona(11656): in retry listener

12-11 17:58:04.898: I/Corona(11656): ended

12-11 17:58:04.898: I/Corona(11656): response high low

12-11 17:58:04.898: I/Corona(11656): IN REAL HANDLER

12-11 17:58:04.898: I/Corona(11656): IS ERROR false

12-11 17:58:04.898: I/Corona(11656): RESPONSE high low

12-11 17:58:04.918: I/Corona(11656): in retry listener

12-11 17:58:04.918: I/Corona(11656): ended

12-11 17:58:04.918: I/Corona(11656): response 

12-11 17:58:04.918: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:04.918: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:04.918: I/Corona(11656):  

12-11 17:58:04.918: I/Corona(11656): IN REAL HANDLER

12-11 17:58:04.918: I/Corona(11656): IS ERROR false

12-11 17:58:04.928: I/Corona(11656): RESPONSE 

12-11 17:58:04.928: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:04.928: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:04.928: I/Corona(11656):  

Running the Get Test Sets again

12-11 17:58:09.898: I/Corona(11656): GET TEST

12-11 17:58:09.898: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=yes&test2=no

12-11 17:58:09.910: I/Corona(11656): GET TEST2

12-11 17:58:09.910: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=boo&test2=hoo

12-11 17:58:09.918: I/Corona(11656): GET TEST3

12-11 17:58:09.918: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=high&test2=low

12-11 17:58:09.948: I/Corona(11656): GET TEST

12-11 17:58:09.948: I/Corona(11656): URL http://192.168.1.6:5723/testNetwork.php?test1=red&test2=green

12-11 17:58:10.068: I/Corona(11656): in retry listener

12-11 17:58:10.068: I/Corona(11656): ended

12-11 17:58:10.068: I/Corona(11656): response 

12-11 17:58:10.068: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:10.068: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:10.068: I/Corona(11656):  

12-11 17:58:10.068: I/Corona(11656): IN REAL HANDLER

12-11 17:58:10.068: I/Corona(11656): IS ERROR false

12-11 17:58:10.068: I/Corona(11656): RESPONSE 

12-11 17:58:10.068: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:10.068: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:10.068: I/Corona(11656):  

12-11 17:58:10.191: I/Corona(11656): in retry listener

12-11 17:58:10.198: I/Corona(11656): ended

12-11 17:58:10.198: I/Corona(11656): response 

12-11 17:58:10.198: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:10.198: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:10.198: I/Corona(11656):  

12-11 17:58:10.198: I/Corona(11656): IN REAL HANDLER

12-11 17:58:10.198: I/Corona(11656): IS ERROR false

12-11 17:58:10.198: I/Corona(11656): RESPONSE 

12-11 17:58:10.198: I/Corona(11656): Notice: Undefined index: test1 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 4

12-11 17:58:10.198: I/Corona(11656): Notice: Undefined index: test2 in C:\app_dev\app_dev\chinesePoker\web\testNetwork.php on line 5

12-11 17:58:10.198: I/Corona(11656):  

12-11 17:58:10.229: I/Corona(11656): in retry listener

12-11 17:58:10.229: I/Corona(11656): ended

12-11 17:58:10.229: I/Corona(11656): response high low

12-11 17:58:10.229: I/Corona(11656): IN REAL HANDLER

12-11 17:58:10.229: I/Corona(11656): IS ERROR false

12-11 17:58:10.229: I/Corona(11656): RESPONSE high low

12-11 17:58:10.248: I/Corona(11656): in retry listener

12-11 17:58:10.248: I/Corona(11656): ended

12-11 17:58:10.258: I/Corona(11656): response red green

12-11 17:58:10.258: I/Corona(11656): IN REAL HANDLER

12-11 17:58:10.258: I/Corona(11656): IS ERROR false

12-11 17:58:10.258: I/Corona(11656): RESPONSE red green

Am I doing something fundamentally wrong in my network.requests with “POST”?  Is it a bug in Android?  Is some corona code not returning the right response from an event handler, or not closing a connection down correctly?

I would sugest loging the requests on the server side to be sure what actualy comes to the server. I doubt that android looses parameters especialy on the URL encoded GET method. So try loging the request URL of GET requests and the body of POST requests on server side. That might give a hint about what’s going on.

I have to mention that I am experiencing some really bizarre network.request behavior as well when running our app on the latest release on our galaxy s3 test phone. Prior to the update not an issue, but post-4.3 update it seems to regularly take two requests to get anything through to our webserver. This doesn’t happen on the simulator just the device.

Just wanted to mention that I am seeing some strange behavior as well so it isn’t just you, I haven’t found a solution yet either. It seems to be an OS issue but if anyone has workarounds in the meantime please share.

Android 4.3 seems to be hyper sensitive to timeouts with web servers.  Your best advice is to watch for the error and try again.

Rob

Finally figured it out.  I wasn’t url encoding parameters that contained spaces.  Got that fixed and now its okay.  I am also retrying all network connections 5 times before sending a message to the user.

P.S. The above network retry code doesn’t work as there is some scoping issues with the counter variable.  

Glad you got it fixed.

Rob

I would sugest loging the requests on the server side to be sure what actualy comes to the server. I doubt that android looses parameters especialy on the URL encoded GET method. So try loging the request URL of GET requests and the body of POST requests on server side. That might give a hint about what’s going on.

I have to mention that I am experiencing some really bizarre network.request behavior as well when running our app on the latest release on our galaxy s3 test phone. Prior to the update not an issue, but post-4.3 update it seems to regularly take two requests to get anything through to our webserver. This doesn’t happen on the simulator just the device.

Just wanted to mention that I am seeing some strange behavior as well so it isn’t just you, I haven’t found a solution yet either. It seems to be an OS issue but if anyone has workarounds in the meantime please share.

Android 4.3 seems to be hyper sensitive to timeouts with web servers.  Your best advice is to watch for the error and try again.

Rob

Finally figured it out.  I wasn’t url encoding parameters that contained spaces.  Got that fixed and now its okay.  I am also retrying all network connections 5 times before sending a message to the user.

P.S. The above network retry code doesn’t work as there is some scoping issues with the counter variable.  

Glad you got it fixed.

Rob