传递时间参数encode转换问题

一、传递时间参数encode转换问题

当涉及到到日期相关的时间参数传递时,我们需要用encode来转换下格式,直接贴代码:

(1)调用节点api:

public String historyMonitorNew() {
		try {
			String nodeIp = getNodeIp();
			
			System.out.println("startTime="+startTime+",endTime="+endTime);
			System.out.println("startEncode="+URLEncoder.encode(startTime,"UTF-8")+",endEncode="+URLEncoder.encode(endTime,"UTF-8"));

			String s = HttpRequest.sendGet("http://" + nodeIp + "/BackManagerNode/VmMonitorApi_historyMonitorNew", 
					"uuid=" + uuid + "&msgType=" + msgType + "&timeType=" + timeType + "&ip=" + ip+"&startTime="+URLEncoder.encode(startTime,"UTF-8")+"&endTime="+URLEncoder.encode(endTime,"UTF-8"));
			
			if (s != null && !"".equals(s)) {
				generateSuccessResponse(JSONObject.fromObject(s));
			} else {
				generateFailResponse("请求失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			generateFailResponse("请求失败");
		}
		return JSON;
}

(2)节点调用接口:

public void historyMonitorNew(){
		JSONObject json = new JSONObject();
			VmHostNetCardBaseClass vmHostNetCardBaseClass = null;
			Ipv4Entity ipv4 =new Ipv4Entity();
			ipv4 = this.vmHostNetCardMsgBaseService.getIp(uuid);
			String hostUseUrl = EnvironmentProperties.envPropertis.getProperty("backmanager.jiankong")
					+ "api/monitor/vmhost/bandwidth";
			JSONObject bandwidth = new JSONObject();
			JSONObject bandwidth2 = new JSONObject();
			if ("5min".equals(timeType)) {
				//查询之前24小时的数据
				String params="vuuid="+uuid+"&type=1"+"&startTime="+URLEncoder.encode(startTime)+"&endTime="+URLEncoder.encode(endTime);
				
				String useStr = HttpRequest.sendGetJson(hostUseUrl,params);
				if (useStr != null && !"".equals(useStr)) {
					JSONObject resultObj = JSONObject.fromObject(useStr);
					if (resultObj.getInt("status") == 1) {
						bandwidth = resultObj.getJSONObject("data").getJSONObject("bandwidth");
						bandwidth2=dealCard(bandwidth,uuid);
					}
				}
				vmHostNetCardBaseClass = new VmHostNetCardMsg5Min();
			}else if ("hour".equals(timeType)) {
				//查询之前7天的数据
				String params="vuuid="+uuid+"&type=2"+"&startTime="+URLEncoder.encode(startTime)+"&endTime="+URLEncoder.encode(endTime);
				String useStr = HttpRequest.sendGetJson(hostUseUrl,params);
				if (useStr != null && !"".equals(useStr)) {
					JSONObject resultObj = JSONObject.fromObject(useStr);
					if (resultObj.getInt("status") == 1) {
						bandwidth = resultObj.getJSONObject("data").getJSONObject("bandwidth");
						bandwidth2=dealCard(bandwidth,uuid);
					}
				}
				vmHostNetCardBaseClass = new VmHostNetCardMsgHour();
			}else if ("4hour".equals(timeType)) {
				//查询之前一个月的数据
				String params="vuuid="+uuid+"&type=3"+"&startTime="+URLEncoder.encode(startTime)+"&endTime="+URLEncoder.encode(endTime);
				String useStr = HttpRequest.sendGetJson(hostUseUrl,params);
				if (useStr != null && !"".equals(useStr)) {
					JSONObject resultObj = JSONObject.fromObject(useStr);
					if (resultObj.getInt("status") == 1) {
						bandwidth = resultObj.getJSONObject("data").getJSONObject("bandwidth");
						bandwidth2=dealCard(bandwidth,uuid);
					}
				}
				vmHostNetCardBaseClass = new VmHostNetCardMsg4Hour();
			}else if ("7day".equals(timeType)) {
				//查询之前一年的数据
				String params="vuuid="+uuid+"&type=4"+"&startTime="+URLEncoder.encode(startTime)+"&endTime="+URLEncoder.encode(endTime);
				String useStr = HttpRequest.sendGetJson(hostUseUrl,params);
				if (useStr != null && !"".equals(useStr)) {
					JSONObject resultObj = JSONObject.fromObject(useStr);
					if (resultObj.getInt("status") == 1) {
						bandwidth = resultObj.getJSONObject("data").getJSONObject("bandwidth");
						bandwidth2=dealCard(bandwidth,uuid);
					}
				}
				vmHostNetCardBaseClass = new VmHostNetCardMsg7Day();
			}
			if (vmHostNetCardBaseClass == null) {
				responseData("获取历史数据,时间类型不正确");
				return;
			}
			
			json.element("bandwidth", bandwidth2);
		
		responseData(json);
}

猜你喜欢

转载自blog.csdn.net/liuyishan1993/article/details/84531146