Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked June 7th 2010 by Ryan Asleson
  • android
  • titanium.network.httpclient
3 Comments
  • Thanks for the in-depth report! I recently made some changes in XHR, are you running from 1.3 or from a recent nightly / git?

    — commented June 8th 2010 by Marshall Culpepper
  • This is from 1.2.1. How do I get 1.3? I just downloaded the new OS X build from the web site and it still says 1.2.1.

    — commented June 9th 2010 by Ryan Asleson
  • 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.

    — commented June 10th 2010 by Ryan Asleson

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});
    
    — answered June 9th 2010 by Nils
    permalink
    1 Comment
    • I had the same Android problem: quotes around username and password prevented the server to read the POST data. No quotes solved the problem. Under iOS, quotes or no quotes were successful. Thanks for the tipp.

      — commented August 5th 2014 by Kai Müller
  • 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.

    — answered June 10th 2010 by Ryan Asleson
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.