我使用以下代码将 JSON 对象发送到 HTTP 服务器。
最主要的是我还必须发送布尔值。
public void getServerData() throws JSONException, ClientProtocolException, IOException {
ArrayList<String> stringData = new ArrayList<String>();
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://consulting.for-the.biz/TicketMasterDev/TicketService.svc/SaveCustomer");
JSONObject json = new JSONObject();
json.put("AlertEmail",true);
json.put("APIKey","abc123456789");
json.put("Id",0);
json.put("Phone",number.getText().toString());
json.put("Name",name.getText().toString());
json.put("Email",email.getText().toString());
json.put("AlertPhone",false);
postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
String response = httpClient.execute(postMethod,resonseHandler);
Log.e("response :", response);
}
但它显示了该行中的异常
String response = httpClient.execute(postMethod,resonseHandler);
as
org.apache.http.client.HttpResponseException: Bad Request
谁能帮我。
错误请求是服务器表示它不喜欢您的 POST 中的某些内容。
我看到的唯一明显的问题是,您没有告诉服务器您正在发送 JSON,因此您可能需要设置一个 Content-Type 标头来指示正文是application/json
:
postMethod.setHeader( "Content-Type", "application/json" );
如果这不起作用,您可能需要查看服务器日志以了解为什么它不喜欢您的帖子。
如果您无法直接访问服务器日志,则需要与服务器所有者联系以尝试进行调试。可能是您的 JSON 格式稍有错误、缺少必填字段或其他类似问题。
如果您无法访问服务器的所有者,则可以尝试使用数据包嗅探器(例如 WireShark)从您的应用程序和成功的 POST 中捕获数据包,然后比较两者以尝试找出答案有什么不同吗。不过,这有点像大海捞针,特别是对于大型物体。
如果您无法获得成功 POST 的示例,那么您就很无聊了,因为您没有参考点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)