我使用以下代码将HTTP控制器发送到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);
}
Run Code Online (Sandbox Code Playgroud)
但它在行中显示异常
String response = httpClient.execute(postMethod,resonseHandler);
Run Code Online (Sandbox Code Playgroud)
如
org.apache.http.client.HttpResponseException: Bad Request
Run Code Online (Sandbox Code Playgroud)
谁能帮我.
Mar*_*son 13
错误请求是服务器说它不喜欢你的POST中的东西.
我能看到的唯一明显的问题是你没有告诉服务器你发送的是JSON,所以你可能需要设置一个Content-Type标头来指示正文是application/json:
postMethod.setHeader( "Content-Type", "application/json" );
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,您可能需要查看服务器日志以查看它不喜欢您的POST的原因.
如果您无法直接访问服务器日志,则需要与服务器的所有者联系以尝试调试事物.可能是您的JSON格式略有错误,缺少必填字段或其他一些此类问题.
如果您无法访问服务器的所有者,您可以尝试使用数据包嗅探器(如WireShark)从您的应用程序和成功的POST中捕获数据包并比较两者以尝试和解决有什么不同吗.这可能有点像在大海捞针,特别是对于大型尸体.
如果你不能得到一个成功的POST的例子,那么你很好,因为你没有参考点.
| 归档时间: |
|
| 查看次数: |
13604 次 |
| 最近记录: |