title: Android - HttpClient JSON POST with Basic Authentication
body: <p>I am trying to implement a simple <code>JSON</code> post to the URL that accepts <code>JSON</code> in body with basic authentication on <code>ANDROID</code>.</p>
  
  <p>I have tried with <code>HttpUrlConnection</code> but I get <code>"unauthorized"</code> and my data gets send to the server. </p>
  
  <p>Another way I tried is using <code>HttpClient</code>, but now I get a different problem. Authentication works perfect but the data is not sent to the server...</p>
  
  <p>Just to be sure, I setup a small test in a simple Java project (not Android Environment).</p>
  
  <p>This is the code that I am using to <code>POST</code> to the server:</p>
  
  <pre><code>DefaultHttpClient httpClient = new DefaultHttpClient();
  ResponseHandler&lt;String&gt; 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);
  </code></pre>
  
  <p>The code in the Java project works perfect.</p>
  
  <p>When I try exact same code in Android, I get <code>internal server error</code> from the server, which means that JSON data has not been received.</p>
  
  <p>I really don't understand why this is working in JAVA but not in Android.</p>
  
  <p>The effect that I would like to achieve, is the following command:</p>
  
  <pre><code>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/
  </code></pre>