import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
/**
* 获取信息
* @author wcl
* @date 2015-09-08
*/
@Service("getInfoService")
public class GetInfoService {
private static final Logger logger = Logger.getLogger(GetInfoService.class);
public void getInfo() {
String getUrl = "http://.....";
logger.debug("GetInfoService debug in ~");
System.out.println("--------------------------" + new Date() + "--------------------------");
try{
String result = sendUrlRequest(getUrl); // 调用
JSONObject json = JSONObject.parseObject(result);
System.out.println("json:" + json);
System.out.println("json_string:" + json.getJSONArray("info"));
}catch (Exception e) {
e.printStackTrace();
}
logger.debug("GetInfoService debug out ~");
}
/**
* 获取信息接口
* @param urlStr
* @return
* @throws Exception
*/
public String sendUrlRequest(String urlStr) throws Exception {
String tempStr = "";
HttpURLConnection url_con = null;
try {
URL url = new URL(urlStr);
StringBuffer bankXmlBuffer = new StringBuffer();
// 创建URL连接,提交到数据,获取返回结果
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent", "directclient");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
bankXmlBuffer.append(inputLine);
}
in.close();
tempStr = bankXmlBuffer.toString();
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
} finally {
if (url_con != null)
url_con.disconnect();
}
return tempStr;
}
}