一个工程中的主要方法:
public Map ky(Map map){
String url = "http://localhost:8080/api/json/ky.action";
Map reMap = new HashMap();
try{
if(map.size() == 0){
reMap.put("outResult", "0");
reMap.put("outResultReason", "无数据需要发送");
return reMap;
}
map.put("typeId", "06");
ArrayList list = (ArrayList)dao.queryMapList("billVerify.getLinkUrl",map);
/*
if(list == null || list.size() == 0){
reMap.put("outResult", "1");
reMap.put("outResultReason", "不调用物流验证接口");
return reMap;
}else{
HashMap linkMap = (HashMap)list.get(0);
url = linkMap.get("linkUrl").toString();
}
*/
HttpClient client = new HttpClient();
//返回结果集
JSONObject resJson = new JSONObject();
PostMethod postMethod = new PostMethod(url);
StringPart strPart1 = new StringPart("param", URLEncoder.encode(JSONObject.fromObject(map).toString(), "utf-8"));
client.getParams().setSoTimeout(15000);
Part[] parts = { strPart1};
//对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(mre);
//执行请求,返回状态码
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
String result = postMethod.getResponseBodyAsString();
resJson = JSONObject.fromObject(result);
if("1".equals(resJson.get("outResult"))){
reMap.put("outResult", resJson.get("outResult"));
reMap.put("outResultReason", resJson.get("outResultReason"));
}else{
reMap.put("outResult", resJson.get("outResult"));
reMap.put("outResultReason", resJson.get("outResultReason"));
}
} else {
System.out.println("接口请求失败。");
reMap.put("outResult", "0");
reMap.put("outResultReason", "请求失败。");
}
}catch(Exception e){
e.printStackTrace();
reMap.put("outResult", "0");
reMap.put("outResultReason", "请求超时。");
}
return reMap;
}
另一个工程中的主要方法:
public String ky(){
Map map = new HashMap();
try {
jsonParam = URLDecoder.decode(param ,"UTF-8");
} catch (Exception e1) {
e1.printStackTrace();
}
if(jsonParam==null){
map.put("outResult", "0");
map.put("outResultReason", "参数:param 为空!");
result = JSONObject.fromObject(map);
return SUCCESS;
}
map = jsonService.ky(jsonParam);
result = JSONObject.fromObject(map);
return SUCCESS;
}