1

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","[email protected]");


    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?

1
  • You need to include the full error (including status code and message) to your question. Also, if you have access to the server, find the corresponding exception in the logs and include that in your question as well. Commented Jun 9, 2013 at 12:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.