I am getting the error bad request
when I try to send a json object with authentication. I had a look at How to send a JSON object over HttpClient Request with Android? but it does not solve my problem.
public void getServerData() throws JSONException, ClientProtocolException, IOException {
ArrayList<String> stringData = new ArrayList<String>();
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
byte[] data = "myusername:mypassword".getBytes("UTF-8");
String encoding = Base64.encodeToString(data, Base64.DEFAULT);
HttpPost postMethod = new HttpPost("http://url/email_check.json");
postMethod.setHeader(HTTP.CONTENT_TYPE, "application/json");
postMethod.setHeader("Authorization", "Basic " + encoding);
JSONObject json = new JSONObject();
json.put("email","somebody@email.com");
postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
String response = httpClient.execute(postMethod,resonseHandler);
Log.e("response :", response);
}
The line String response = httpClient.execute(postMethod,resonseHandler);
causes a ClientProtocolException
as follows:
cause: org.apache.http.client.HttpResponseException: Bad Request
detailMessage: Bad Request
stackState: (id=830103290336)
stackTrace: null
statusCode: 400
Any idea what is wrong?