当我尝试发送带有身份验证的json对象时,我收到错误bad request
。我看了How to send a JSON object over HttpClient Request with Android?,但它没有解决我的问题。
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);
}
行String response = httpClient.execute(postMethod,resonseHandler);
导致ClientProtocolException
如下:
cause: org.apache.http.client.HttpResponseException: Bad Request
detailMessage: Bad Request
stackState: (id=830103290336)
stackTrace: null
statusCode: 400
知道出了什么问题吗?