Android: Problems Posting Data with HTTPClient
Hello,
I want to use the Titanium.Network.HTTPClient
object to network an application with a remove server.
My first task is to implement a login screen that uses the remote server to validate the user's username and password. I use the following line to post the entered username and password to the remove server:
xhr.send({"username": username, "password": password});
I use Java EE to implement the remote server. On the server side I can use this code to retrieve the username and password from the request:
String username = request.getParameter("username");
String password = request.getParameter("password");
When the request is sent using the iPhone simulator I can successfully retrieve the username and password from the request. However, when the request is sent from Android, both the retrieved username and password are null
.
Digging deeper, I decided to manually read the request's body, as this will tell me the actual contents of the request body. For iPhone, the request body looks like this:
username=iphoneuser&password=iphonepassword
This is exactly what I expect it to be: a query string that's enclosed in the request body.
Doing the same for Android, I find that the request body looks like this:
--MsuFIjcV2DY7bvFKN7_AfNya4Pbtf8
Content-Disposition: form-data; name="password"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: bit
androidpassword
--MsuFIjcV2DY7bvFKN7_AfNya4Pbtf8
Content-Disposition: form-data; name="username"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: bit
androiduser
--MsuFIjcV2DY7bvFKN7_AfNya4Pbtf8--
While technically the username and password are included in the request body, it's not at all in a form that I expect to see it in, nor is this easily parsed.
Is this expected? Is there a reason why the HTTPClient
on Android doesn't send a POST as a query string like iPhone does? Writing a parser to get the request parameters from Android won't be easy, and it would be nice if I didn't have to. This doesn't seem to follow how the XMLHttpRequest
object works, which is apparently what HTTPClient
is patterned after.
Any comments or suggestions are greatly appreciated.
Thank you!!
-Ryan
2 Answers
-
under 1.3.0 xhr works on both devices in the exact same way for me. I use PHP on the server side though.
try to use NO quotes on the JS array/object keys:
xhr.send({username: username, password: password});
-
Marshall – FYI I verified that I'm using the 1.3 SDK and the behavior is the same as what I previously listed. I was able to build a custom request wrapper that lets me read the values on the server side the same as I do for the iPhone, but it's still a little strange.