1

I have a login that needs an email and password.

If I hit it from Postman rest client with this:

www.site.com/login.json?session[email]=bob@gmail.com&session[password]=password

The server (Rails) will read it just fine, like this:

{"session"=>{"email"=>"bob@gmail.com", "password"=>"password"}, "action"=>"create", "controller"=>"sessions", "format"=>"json"}

But, if I send in the same thing with HTTPGet from android, like this:

            HttpClient httpclient = new DefaultHttpClient();

            //get the parameters
            String email = params[0];
            String password = params[1];

            String url = "http://www.site.com/login.json?session[email]=" + email + "?session[password]=" + password;

            HttpGet httpget = new HttpGet(url);

            httpget.setHeader("Accept", "application/json");
            httpget.setHeader("Content-type", "application/json");
response = httpclient.execute(httpget);

The server doesn't recognize the parameters, and I end up with an empty json object like this:

{"session"=>{}, "action"=>"create", "controller"=>"sessions", "format"=>"json"}

Anybody know how to form the parameters in this HTTPGet call in android to work like it will in a the rest client call? Thanks!

botbot
  • 7,299
  • 14
  • 58
  • 96

4 Answers4

6

It seems you have a typo in URL and params get messed up. This line:

String url = "http://www.site.com/login.json?session[email]=" + email + "?session[password]=" + password;

should be

String url = "http://www.site.com/login.json?session[email]=" + email + "&session[password]=" + password;

Note that I've replaced ?session[password]= with &session[password]=.

Ilya Khokhryakov
  • 3,686
  • 1
  • 19
  • 22
0

Do you need to send USER-AGENT or other HTTP headers for it to be valid? Did you trace the HTTP connection with Fiddler2 or another HTTP trace? That ? instead of a & is probably it though.

For a type-safe REST client for Android try: http://square.github.io/retrofit/ It works very nice and you don't have to worry about manual parsing.

Tim Spann
  • 517
  • 2
  • 6
0

One thought I have is that you could try encoding the query string of your URL.

String query = URLEncoder.encode("session[email]=" + email + "?session[password]=" +                 password", "UTF-8");
String url = "http://www.site.com/login.json?" + query;
HttpGet httpget = new HttpGet(url);
....
nmorenor
  • 177
  • 12
-1

* Replace the ?session in the URL with &session as its a mistake..

If the issue still persists, then try using a POST request as:
- in the first place exposing the email and password that way isn't safe practice
- and POST variables can be easily be handled in Rails controllers using the params tag eg. params[:tag] where tag is the key in the Json object.