bad request
当我尝试发送带有身份验证的 json 对象时出现错误。我查看了如何使用 Android 通过 HttpClient 请求发送 JSON 对象?但这并不能解决我的问题。
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);
导致 aClientProtocolException
如下:
cause: org.apache.http.client.HttpResponseException: Bad Request
detailMessage: Bad Request
stackState: (id=830103290336)
stackTrace: null
statusCode: 400
知道有什么问题吗?