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;
}
HTTP接口客户端调用程序
最新推荐文章于 2024-06-14 21:35:46 发布