The comment section for this API has been closed. Please continue the discussion on the Corona forums.
http://developer.anscamobile.com/reference/index/networkrequest [import]uid: 7559 topic_id: 24299 reply_id: 324299[/import]
Hi,
POST request seems not to work on android.
Tryed 704a, b, 767, 786, 812.
here the code:
local json = require "json"
local mime = require "mime"
local params = {
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
body = "identifier=username1&password=password1"
}
local function networkListener(event)
if ( event.isError ) then
print( "ERRORE NETWORK")
else
print (event.response)
end
end
network.request( "http://username2:password2@api.example.com/login", "POST", networkListener,params)
everything goes fine on simulator moving so samsung galaxy s2 android 2.3.3 and asus transformer adnroid 4.0.3 it stops working.
Removing the /login at the end of the url resolve the problem but defeat the object avoiding login.
Seems a bug. [import]uid: 104317 topic_id: 24299 reply_id: 106375[/import]
It’s hardly a bug in Corona. I have been sending post requests happily for the past month. There are two possibilities there:
- Put a Content-Length header in there as well with the size of the string you’re sending in the body
- Listen for the event.status and the response message to see what the problem is
PS: Bad idea to send requests in the form username:password@domain. It’s a security breach and in fact it could be the reason why the request fails. [import]uid: 61899 topic_id: 24299 reply_id: 106404[/import]
Same problem.
I think this is not a “POST” request problem but “cookie” problem.
I made login process using http cookie, it works fine at simulator and iphone.
But it’s not working at android.
After web server sets cookie, network.request api call doesn’t have cookie set.
It’s big issue. fast bug fix needed. [import]uid: 155824 topic_id: 24299 reply_id: 112281[/import]
Is there anyway to get a return from a network.request, or rather from the listener? I would like to do something like
[code]
local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
return false
else
print ( "RESPONSE: " … event.response )
return true
end
end
if network.request( “https://www.scoreoid.com/api/getPlayer”, “POST”, networkListener, params) then
print(“USERNAME WAS FOUND!”)
return true
end
[/code] [import]uid: 129450 topic_id: 24299 reply_id: 112626[/import]
That doesn’t work as you probably have guessed. It doesn’t because the networkListener returns true
or false
to the calling function/context, which in this case is the network.request function. Since it’s not possible to assign a network.request to a variable because the function doesn’t return anything those return booleans from the networkListener get lost in a sort of limbo waiting to be garbage collected.
To work around this you can have a variable defined outside on the networkListener whose value is set to either true or false (or whatever) when the networkListener gets a response. Then you have to check the value of this variable in another function with a timer. It’s a bit troublesome to implement but it works. Another solution is to use the LuaSocket library since then the requests are synchronous and it is possible to wait until a response is received before returning either true or false. The problem with that approach is that the whole application will hang until a response is received (or the connection times out). [import]uid: 61899 topic_id: 24299 reply_id: 112644[/import]
Thanks for the reply CluelessIdeas.
None of your solutions feels appropriate for my game but I came up with two ideas for solving my problem, altough I later realized they won’t work.
Idea 1.
I was thinking that I could have a local function inside the called function that would return the value but then I realized it is exactly the same as before.
[code]
function highScore:getPlayer( username)
local function networkListener2( event )
if ( event.isError ) then
print( “Network error!”)
else
print ( "RESPONSE: " … event.response )
local t = json.decode( event.response)
return t
end
end
postData = “api_key=” … mApi_key … “&game_id=” … mGame_id … " &response=json&
username = " … username
local params = {}
params.body = postData
network.request( “https://www.scoreoid.com/api/getPlayer”, “POST”, networkListener2, params)
end[/code]
Idea 2.
Then I was thinking I could send a function reference to the listner so that instead of returning a value, it calls the function wich sent the network request. This works! However, I want to return the value to the function that called getPlayer( ) but If I do this, it will just return to the networkListener which mean I have to do this for one more step (or more).
[code]
local function networkListener( event, refFunction )
if ( event.isError ) then
print( “Network error!”)
if refFunction then
refFunction(nil, true, nil)
end
return false
else
print ( "RESPONSE: " … event.response )
if refFunction then
refFunction(nil, true, event.response)
end
return true
end
end
function highScore:getPlayer( username, returned, response)
if returned then
if response then
return response
else
return false
end
end
postData = “api_key=” … mApi_key … “&game_id=” … mGame_id … “&response=json&username=” … username
local params = {}
params.body = postData
network.request( “https://www.scoreoid.com/api/getPlayer”, “POST”, function(event) networkListener(event, highScore.getPlayer) end, params)
end[/code]
I don’t get why you just can’t get a return value from a listener or why network.request doesn’t return something to begin with seeing as the user would most likley want to use the response event. [import]uid: 129450 topic_id: 24299 reply_id: 113185[/import]
Realized I could just send a reference from the original function which would then get called directly from the network listener, it seems that this is the simplest to do it this far. [import]uid: 129450 topic_id: 24299 reply_id: 113187[/import]
Hi there,
Pretty new to Corona and LUA, so please excuse my NOOB comments if judged as such.
It does seem a big weakness of the Corona SDK that there is no “completed” event with these network calls.
Given that Corona claims that it is useful for business apps, and I would think that a lot of business apps revolve around regular access to databases, the fact that the network.download call is so limited would suggest that the Corona team might consider expanding this call’s functionality somewhat.
Maybe it does and I’ve missed something here…
JB
[import]uid: 58328 topic_id: 24299 reply_id: 115903[/import]
We have the same problem to you. Our server cannot receive POST data with https protocol.
Is there any idea? [import]uid: 9190 topic_id: 24299 reply_id: 116002[/import]
@webpurchases and khanh
I’m a genuine newb also, but using the code posted by Satheesh as a template, network.request works great and can use a lot of different params that go along with your server php.
I used the code to verify IAP receipts and have modified it to upload and download a variety of data/variables.
I really struggled at first because I knew “0” php, but have been able to get a few things done now.
@khanh, FYI, https works with GoDaddy, I’m developing for iOS though and not Android.
Here’s the link to Satheesh’s code, it was the perfect starting point for me.
Download the Github
Nail [import]uid: 106779 topic_id: 24299 reply_id: 116188[/import]
Just spent a few days digging into the problem with cookie not being set on network.request running in the Android platform.
This is a feature that they needed to implement in the SDK. While they are at it, persistent cookie support for webPopUp should be there also. Otherwise you would have your user log into a remote service, then whenever a webPopUp appears the user will need to log in all over again. That just wouldn’t work.
For now there is a quick fix for what storing cookie information. First, login to the web server through a regular browser and collect the cookie information for the session id, or whatever information is required. For example:
yourservice.com
name: client\_sid
value: 123456789
domain: .yourservice.com
path: /
The only thing important is to know is the name for storing session info, in this case, it’s “client_sid”.
Next you’ll need the server to pass down that cookie value to the android client, since Corona’s network.request doesn’t parse header info.
Inside your client’s login manager, save the cookie info.
Now for every subsequent network.request you make, include the following:
*assuming your session ID is saved as userdata.sid
local headers = {}
local params = {}
if (userdata.sid) then
headers["Cookie"] = "client\_sid=" .. userdata.sid .. ";"
params.headers = headers
end
network.request(url, "GET", networkListener, params)
As you can see, “client_sid” is the cookie name that the server assigns, and userdata.sid is what’s collected when you first made your login request. No need to worry about domains, paths, or stuff that are for browsers. Do not leave any spaces between the equal sign or underscores will be inserted for you and mess up the values.
If you’re trying to access a server that you don’t have control over and they don’t provide session info outside of the cookie then you’re dead in the water, unfortunately. [import]uid: 159488 topic_id: 24299 reply_id: 116799[/import]
Hi,
It seems i can’t send any POST request on android in build 840
Anyone having similare issue ? isError is always set to true
[import]uid: 79037 topic_id: 24299 reply_id: 121997[/import]
Just noticed it’s bug #15411 [import]uid: 79037 topic_id: 24299 reply_id: 122017[/import]
The Android post error is only there if you try to set the content-length (as mentioned in the bug report). If you don’t set it, Corona will set it for you and the post will work correctly.
We have now released build 894 and you should be using that instead of 840.
-Tom [import]uid: 7559 topic_id: 24299 reply_id: 122037[/import]
Hi,
It seems i can’t send any POST request on android in build 840
Anyone having similare issue ? isError is always set to true
[import]uid: 79037 topic_id: 24299 reply_id: 121997[/import]
Just noticed it’s bug #15411 [import]uid: 79037 topic_id: 24299 reply_id: 122017[/import]
The Android post error is only there if you try to set the content-length (as mentioned in the bug report). If you don’t set it, Corona will set it for you and the post will work correctly.
We have now released build 894 and you should be using that instead of 840.
-Tom [import]uid: 7559 topic_id: 24299 reply_id: 122037[/import]
@Tom
I have encounter an emergency issue
After my iphone upgrade to IOS6
I have found sometimes i use
network.request()
It would crash my app.(not every time)
or hang there.
It’s emergency. My app always connect to internet.
Fetch data and store in local device.
Can u guys check this issue ASAP?
Anybody got same issue?
In IOS5 is totally okay, after i upgrade to IOS 6.
U can try this using my app in different IOS
link is here
http://itunes.apple.com/tw/app/le-tou-dui-dui-jiang/id516551775?mt=8 [import]uid: 25057 topic_id: 24299 reply_id: 125803[/import]
We don’t have any reports about network.request crashing on iOS6. You need to file a bug report and supply code that reproduces the problem. That’s the only way we can help. I tried to find your program in the US iTunes APP store and couldn’t. The link you gave me is in Chinese so I didn’t know what to search for. I saw “Billion Lottery” on your support page (Facebook) but no search results when I searched the App store.
Have you tried connecting to Xcode Console when you run your program? Any crash should show something in the console. If you see something with “PCall” error, that means it found a Lua runtime error. If you build with your developer profile and run it until it crashes, it should give you the file and line number of the error. If it gives you a crash log instead, that may be harder to find.
Are you playing audio (sounds) when the game crashes? There are some reported iOS6 audio problems. These are new Apple bugs.
There is a known Apple bug with GameCenter and apps that run only in landscape mode. It looks like your game runs in portrait mode.
Sorry I can’t help you more. [import]uid: 7559 topic_id: 24299 reply_id: 125848[/import]