HTTP接口客户端调用程序

public static void main(String[] args) throws IOException {
		/*String httpUrl = "http://t.s.bdp.jd.com/service/predict/interface1?sid=mysqlserver02" +
				"&appCode=consumer01&institute=611&distributionCenter=9&warehouseId=0&predictDate=2013-06-24&days=2&timeBucket=8:00-12:00,13:00-18:00&requestTimeDSP=1374024926984" +
				"&token=JAYmFO%2FpqQi6fl05g0r4sMC8SWR2%2B7x%2FY3W%2FyfA0FUHYQ8tBG%2FZHkPsaj0XxH%2FcvkJ%2BXcQQVtpth%0D%0AxpZYQMGeevxx48hSldGU8y7XInMvq9mlx9nzk9TCsuDXxDR1SgvpDqwEdL1iZoBe3F0qS2wphu0q%0D%0A4BuU1YHgBM3qcjisCSWQ%2FaEbxGUZ76kPksnOCTPN";*/
		String httpUrl = "http://t.s.bdp.jd.com/service/predict/interface1?sid=mysqlserver02&appCode=consumer01&institute=10&distributionCenter=10&warehouseId=1&predictDate=2013-06-24&days=3&timeBucket=08:00-12:00%2c13:00-18:00%2c18:00-23:00&requestTimeDSP=1374719413233&token=98f95dc74f2b7a40b08485be49951f58";		                
		URL url = new URL(httpUrl);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5* 1000);//设置连接超时
		conn.setRequestMethod("GET");//以get方式发起请求
		if (conn.getResponseCode() != 200){
			 throw new RuntimeException("请求BI链接失败");
		}
		InputStream is = conn.getInputStream();//得到网络返回的输入流
		String result = readData(is, "UTF-8");
		System.out.println(result);
		BiResult bi = JsonUtil.fromJson(result, BiResult.class);
		System.out.println(bi.toString());
		//String jsonResult = "{'code':0,'message':'成功','warehousePredictResponse':{'dayDetailList':[{'alldayPredictOrdnum':1,'predictDate':'2010-09-09','workPeriodList':[{'firstWorkPeriod':'1','firstWorkPeriodPred':'1'},{'firstWorkPeriod':'13:00-14:00','firstWorkPeriodPred':'1'}]},{'alldayPredictOrdnum':0,'predictDate':'2010-09-10','workPeriodList':[]}],'days':2,'distributionCenter':'1','institute':'1','predictDate':'2010-09-09','timeBucket':'19:00-20:00,21:00-23:00','warehouseId':1}}";
		//JSONObject jsonObject = JSONObject.fromObject(jsonResult); 

		conn.disconnect();     


    }
	
	private static String readData(InputStream inSream, String charsetName) throws IOException{
	    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	    byte[] buffer = new byte[1024];
	    int len = -1;
	    while( (len = inSream.read(buffer)) != -1 ){
	      outStream.write(buffer, 0, len);
	    }
	    outStream.close();
	    inSream.close();
	    byte[] data = outStream.toByteArray();
	    
	    String dataStr = new String(data, charsetName);
	    return dataStr;
	  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值