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/