【发布时间】:2015-03-23 07:46:31
【问题描述】:
我正在尝试将 JSON 数据发布到我的 API。但执行后我得到以下结果:
{"name":"Corporate","addr":"Unknown","area":"Unknown","cityId":10,"phone":"--","fax":"--","wooqStoreId":1}]
Response 2 >>{"message":"Blank String","result":"Error","resultCode":"RESULT_CODE_002"}
true
前 2 行显示我的 JSON 字符串和 响应 2 是我收到的消息。当我收到状态码 200 时,这应该是一条成功消息。
public static boolean pushtoAPI(String url, String jsonObject) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost request = null;
HttpResponse response = null;
String postUrl = getHostUrl() + url;
try {
request = new HttpPost(postUrl);
StringEntity postingString = new StringEntity(jsonObject.toString());
postingString.setContentType("application/json;charset=UTF-8");
postingString.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;charset=UTF-8"));
request.setEntity(postingString);
request.setHeader("Content-type", "application/json");
String custom_cookie = ConstantUtil.authCookie(ConstantUtil.getLoginJsessionId());
request.setHeader("Cookie", custom_cookie);
response = client.execute(request);
System.out.println("Response 2 >>" + EntityUtils.toString(response.getEntity()));
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println("true");
return true;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
} finally {
request.abort();
}
return false;
}
【问题讨论】:
-
显示服务器端 coe。
-
为什么是javascript
tag。 -
如果您不介意使用外部库,我可以向您展示一种非常简单的方法!!!
-
@RequestMapping(value = "/sdk/manage.do", method = RequestMethod.POST) public ModelAndView Function(@RequestParam(value = "data", required = false) String dataObj, HttpServletRequest request ) { if (null == dataObj || (null != dataObj && "".equals(dataObj.trim()))) { return this.createJsonResponseMessage("Error", "Blank String", RESULT_BLANK); } ..................... }
-
是的,我不介意使用外部库。
标签: java javascript android httpclient