13

I am trying to implement a simple JSON post to the URL that accepts JSON in body with basic authentication on ANDROID.

I have tried with HttpUrlConnection but I get "unauthorized" and my data gets send to the server.

Another way I tried is using HttpClient, but now I get a different problem. Authentication works perfect but the data is not sent to the server...

Just to be sure, I setup a small test in a simple Java project (not Android Environment).

This is the code that I am using to POST to the server:

DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler<String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://localhost/api/v1/purchase/");    
postMethod.setEntity(new StringEntity("{\"amount_adult\" : 1, \"object_id\" : 13}"));
postMethod.setHeader( "Content-Type", "application/json");
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
postMethod.setHeader("Authorization", authorizationString);
String response = httpClient.execute(postMethod,resonseHandler);
System.out.println("response :" + response);

The code in the Java project works perfect.

When I try exact same code in Android, I get internal server error from the server, which means that JSON data has not been received.

I really don't understand why this is working in JAVA but not in Android.

The effect that I would like to achieve, is the following command:

curl --dump-header - -H "Content-Type: application/json" -X
POST --data '{"amount_adult":1, "object_id":13}' 
--user travelbuddy:travelbuddy http://localhost/api/v1/purchase/
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
user1674948
  • 171
  • 1
  • 1
  • 5

4 Answers4

28
String authorizationString = "Basic " + Base64.encodeToString(
    ("travelbuddy" + ":" + "travelbuddy").getBytes(),
    Base64.DEFAULT);

...

The solution is a bit simpler actually:

    String authorizationString = "Basic " + Base64.encodeToString(
        ("travelbuddy" + ":" + "travelbuddy").getBytes(),
        Base64.NO_WRAP); // <=== Do not add '\n'
Dmitry T.
  • 673
  • 7
  • 7
4

Finally, everything is sorted. Anyone that might experience this problem following is the solution.

String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
postMethod.setHeader("Authorization", authorizationString);

This Base64.encodeToString appends an extra "\n" to the end of the authorization string. Then on the server next line after "\n" character is treated as body of the request and of course incorrect.

By replacing all new lines in authorization string will solve the issue:

    String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
authorizationString.replace("\n", "");
    postMethod.setHeader("Authorization", authorizationString);

Easy fix but hard to find ;-)

user1674948
  • 171
  • 1
  • 1
  • 5
2

I think the simplest way is:

postMethod.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(user, pass), "UTF-8", false));
Till - Appviewer.io
  • 4,529
  • 1
  • 31
  • 35
0

I think you shouldn't use localhost in url

secondflying
  • 871
  • 1
  • 7
  • 16
  • Thank you for your reply, I just illustrated in this code "localhost", in reality I use full path of the domain. – user1674948 Sep 20 '12 at 21:28
  • From your point of you, is this correct way of doing JSON body post to the server? Maybe it is just an Emulator that is not working? – user1674948 Sep 20 '12 at 21:29
  • Your code is right. If you are testing in Emulator, use the IP address 10.0.2.2 instead. Refer http://developer.android.com/tools/devices/emulator.html#networkaddresses. – secondflying Sep 21 '12 at 06:30
  • Actually I am not using localhost or any local network address. I am using proper hosted domain name, which is available publicly. Could that be something with Emulator? I just do not have phone to test. And I am out of ideas what is happening... – user1674948 Sep 21 '12 at 21:56
  • Make sure you can access internet in Emulator. You can view google website in browser to test it. If you can't access internet, refer http://stackoverflow.com/questions/2039964/how-to-connect-android-emulator-to-the-internet – secondflying Sep 22 '12 at 11:43
  • Yes, I tried, I can access internet. By the way, I spotted one weird behaviour. This code Does work if I supply Social AUTH token to authorise, instead of BASIC auth. It is not working when I use BASIC. Here is my code to Add AUTH, maybe something with it? *BASIC Auth works for any other call that requires Authentication, only fails to post JSON body with BASIC. – user1674948 Sep 22 '12 at 12:28
  • public static void AddAuth(HttpEntityEnclosingRequestBase method) { if(!_isFacebookAuthorized) { String authorizationString = "Basic " + Base64.encodeToString(new String(_username + ":" + _password).getBytes(), Base64.DEFAULT); method.setHeader("Authorization", authorizationString); } else { String authorizationString = _username + " " + _accessToken; method.setHeader("X-OAuth-Authentication", authorizationString); } } – user1674948 Sep 22 '12 at 12:28
  • I'm sorry I can't help. Or you can set up a Proxy(e.g. fiddler2) to capture http request/response and may find what's wrong with it :) – secondflying Sep 22 '12 at 12:44
  • Thanks for you help anyways. Ill publish my app now, and go to the phone shop to test :-) Maybe it is just an Emulator... – user1674948 Sep 22 '12 at 13:43