java调用接口(跨域调用函数)

一个工程中的主要方法:

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;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值