I am I missing something using network.request for Android

I’m porting my iOS App to Android ahead of a commercial project :slight_smile: I’m using

network.request( url, method, listener [, params] )

to post to a web service. Is there anything I need to do specifically for Android? I have included the following in the build settings:

androidPermissions =
{
“android.permission.INTERNET”,
},

Code works on a published iOS App and the Corona Mac Android Simulator BUT gives a ( event.isError ) is true on device

Any help gratefully received! [import]uid: 137150 topic_id: 27244 reply_id: 327244[/import]

Hello Dominic,

This typically happens to Corona developers who forget to set their HTTP headers in their network requests/downloads. iOS and OS X operating systems (via the Cocoa framework) automatically set some headers for you if you do not set any, while Android and Windows operating systems do not. So, my recommendation is to find out what HTTP headers this server expects you to set.

If it helps any, Apple sets the following headers for you if you do not set them…
[lua] ACCEPT */*[/lua]
[lua] ACCEPT-LANGUAGE en-us[/lua]
[lua] ACCEPT-ENCODING gzip, deflate[/lua]

Perhaps the server you are trying to communicate expects some of the above headers? [import]uid: 32256 topic_id: 27244 reply_id: 110721[/import]

Thanks, I tried this using the following syntax

headers[“ACCEPT”] ="*/*" --added for android
headers[“ACCEPT-LANGUAGE”] =“en-us” --added for android
headers[“ACCEPT-ENCODING”] = “gzip; deflate”

Now the App goes blank screen and sometimes reappears with my error message.

Is the last line correct, I assumed I needed a semi collon. TBH I’m not a web programmer.

One thing I noticed when installing the app on the Question “Do you want to install this application?” if I press the Show All - the Network Communication - Show Network State has a DOT in front instead of a tick. Am I missing something there? looking through the docs today there was an old post about requesting that Apps that didn’t require these warnings not to have them, i.e. you needed to switch something on [import]uid: 137150 topic_id: 27244 reply_id: 110728[/import]

If the screen is going blank, then that suggests that you have a Lua runtime error. Like you might be making a call to the network API wrong. Have a look at the code example at the bottom of the following web page…

http://developer.anscamobile.com/reference/index/networkrequest
[import]uid: 32256 topic_id: 27244 reply_id: 110732[/import]

Non of the above helped me!

The code works on iOS and Android in the simulator but on the device I get
event.isError is true on the networkListener

the response is

java.lang.DOUBLENETWORK.error. Not sure if this is actually a valid response that can help debug the code or wether the response isn’t setup up as the event returns with the initial error

If anyone can help please reply

[import]uid: 137150 topic_id: 27244 reply_id: 110852[/import]

I recommend that you test our sample apps “Networking/AsynchHTTP” and “Networking/AsynchImageDownload” that is included with the Corona SDK on your Android device. This is to prove that network communications work on your Android device. If they fail, then you know that your device is the culprit. If these sample apps succeed, then it is a communications issue with the server and you need to find out what “exactly” needs to be in that HTTP request packet that you are sending, including the HTTP headers. I don’t think anyone else can help you any further than that. [import]uid: 32256 topic_id: 27244 reply_id: 110900[/import]

Hi Joshuha

I have run the samples and android device is communicating OK, I can’t run the POST example as I don’t have a local webserver running.

I’ve added the 3 headers you suggested with the correct syntax, I have also added a bunch of other headers as TBH I’m clutching at straws here.

The webservice is being accessed by our published iOS App so I know that’s working, the same LUA/Corona code fails when running on an Android device. I have a REST code example running correctly accessing AmazonAWS (Bob Dickenson) so I know that it can work correctly.

The developer responsible for the web service has suggested running the .apk file on the Google ANDROID simulator and then sniffing out what is different between the Android and iOS builds.

I freely admit I’m not an expert in web technologies, Is this the next step?
I have also posted for paid help on Marketplace. Anything else you can suggest?
[import]uid: 137150 topic_id: 27244 reply_id: 111221[/import]

You might want to double check that you are using the newest daily build. The release version, build 704, for Android did not support all HTTP methods/verbs such as PUT, DELETE, and HEAD.

Also, try running “adb logcat” or “ddms” while your Android device is connected to your computer via USB to view a real-time log of what’s going on in your app. Check to see if any errors or warnings are occurring. [import]uid: 32256 topic_id: 27244 reply_id: 111246[/import]

I’ve gone back a few steps to isolate my webservice call and also run this sample code

network.request(“http://127.0.0.1/formhandler.php","POST”, networkListener, params)

On the corona emulator I get an error “Could not connect to server” which is CORRECT as I’m not running a local PHP script

On the Android App I get The same arror as before “Java.lang.Double…”

Basically the code doesn’t appear to work on the handset…
What can I try? I’m using build 828

[import]uid: 137150 topic_id: 27244 reply_id: 111428[/import]

Can you copy and past the exact error messages that you are seeing via “adb logcat” please? The “java.lang.DOUBLENETWORK.error” can’t be the exact spelling of the error. That error message doesn’t make any sense. [import]uid: 32256 topic_id: 27244 reply_id: 111431[/import]

I fixed the issue by rolling back to the last release build.

I ditched all my code and ran a sample like the following to confirm. Now I’ve got to find out which daily build broke the code as there’s a feature that also need that is missing from the release build.
Well at least it is working, the error was when reading the response on ( event.isError ) java.lang.DOUBLE

here is some test code that shows the issue on the daily build (not sure which build after the release build)

Joshua I appreciate your help before.

local asynch_http = require(“async_http”)
local http = require(“socket.http”)

left = display.contentWidth*0.1
top = display.contentHeight*0.1
t_width = display.contentWidth*0.8
t_height = display.contentHeight*0.8

barText = native.newTextBox( left, top, t_width, t_height )
barText.text = “WAITING”

barText:setTextColor(0,0,0)

local function networkListener( event )

if ( event.isError ) then
print( “Network error!”)

response = ( "ERROR: " … event.response )
else
response = ( "RESPONSE: " … event.response )
end

print (response)
barText.text = response
end

local function testWebservice()
headers = {}

body = “sample post body2”
headers[“POST”] ="/sectv.asmx HTTP/1.1"
headers[“Content-Type”] = “text/xml; charset=utf-8”
headers[“Content-Length”] = string.len(body)
headers[“Date”] = os.date("!%a, %d %b %Y %H:%M:%S GMT")
headers[“Accept”] ="*/*" —“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”
headers[“Accept-Language”] =“en-GB,en-US” --;q=0.8,en;q=0.6"
headers[“Accept-Encoding”] = “gzip,deflate,sdch”
headers[“Connection”] = “keep-alive”
headers[“Cache-Control”] = “max-age=0”
headers[“User-Agent”] =“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML,like Gecko) Chrome/20.0.1132.27 Safari/536.11”

params = {} --header and body are held in this table
params.headers = headers --variables go into the table
params.body = body

network.request( “http://posttestserver.com/post.php?dir=mydir”, “POST”, networkListener,params )

end

testWebservice()

[import]uid: 137150 topic_id: 27244 reply_id: 111548[/import]