请求外部接口数据

@RequestMapping(value = "/getWssc", produces = "text/html;charset=UTF-8")
@ResponseBody
public void getWssc() {
	String path = null;
	String saasdm=“”;//请求参数
	String data="";
	//获取数据请求地址
	 try {
         Properties p = PropertiesLoaderUtils.loadAllProperties("/global.properties");
         path = p.getProperty("datawssc");//外部接口地址
     } catch (IOException e) {
         e.printStackTrace();
     }
        BufferedReader in = null;
        StringBuffer result = null;
        try {
			URL url = new URL(path+"?saasdm="+saasdm);
			 //打开和url之间的连接
			 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			 connection.setRequestProperty("Charset", "utf-8");
			 connection.connect();
	            result = new StringBuffer();
	            //读取URL的响应
	            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            String line;
	            while ((line = in.readLine()) != null) {
	                result.append(line);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }finally {
	            try {
	                if (in != null) {
	                    in.close();
	                }
	            } catch (Exception e2) {
	                e2.printStackTrace();
	            }
	        }
        //获取返回数据
       data=result.toString();
		Gson gson = new Gson();
		 try{
			Map<String,Object> dwMap = gson.fromJson(data, Map.class);//将 json格式数据转化map格式
			 String dw= dwMap.get("numberId")+"";//获取返回数据中的值
			 List<Map<String,Object>> dwList = gson.fromJson(dw, List.class);
			 //将数据插入到本地数据库
			 boolean flag = userDao.doInsertBmxx(dwList);
		 }catch(Exception e){
			e.getMessage();
		 }
 }

版权声明:本文为cjcxy_202011原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。