接口读取数据(json)并写入本地
Posted weixc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口读取数据(json)并写入本地相关的知识,希望对你有一定的参考价值。
package com.fykj.service.bike; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import com.fykj.dao.ParkInfo.ParkInfoMapper; import com.fykj.entity.ParkInfo.ParkInfo; import com.fykj.util.HttpUtil; import com.fykj.util.PropertyLoader; import net.sf.json.JSONNull; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @Service("ParkInfoService") @Component public class ParkInfoService { @Autowired private ParkInfoMapper parkInfoMapper; /** * 停车点 */ @SuppressWarnings("static-access") //@Scheduled(cron = "0 0 * * * ?")//一个小时一次 //@Scheduled(cron = "0 0 */2 * * ?")//2个小时一次 @Scheduled(cron = "0 0/60 * * * ? ")//一分钟一次 public void addParkInfo() throws Exception { PropertyLoader properties = new PropertyLoader("parkInfo.properties"); // System.out.println("当前时间戳:" + new Date().getTime()); // System.out.println("昨天时间戳:" + (new Date().getTime() - 86400000L));//86400000L long t1 = (new Date().getTime()); long t2 = (new Date().getTime()) - 3600000L; System.out.println("当前时间戳:" + t1); System.out.println("一个小时前时间戳:" + t2 ); String param = "{\"key\":\"" + properties.getProperty("key") + "\"," + "\"reqTime\":\"" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "\"," + "\"requestResource\":\"" + properties.getProperty("requestResource") + "\"," + "\"createTimeFrom\":\"" //+ properties.getProperty("createTimeFrom") + "\"," + "\"createTimeTo\":\"" + t2 + "\"," + "\"createTimeTo\":\"" + t1 + "\"}"; //+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "\"}"; String content = HttpUtil.sendPost(param, properties.getProperty("queryParkInfoUrl")); //System.out.println("数据3:内容"+content); if (StringUtils.isEmpty(content)) { return; } if (content.indexOf("[") < 0 || content.indexOf("]") < 0) { System.out.println("数据错误!!!!"); return; } content = content.substring(content.indexOf("["), (content.indexOf("]") + 1)); JSONArray jsonArr = new JSONArray(); jsonArr = jsonArr.fromObject(content); ParkInfo parkInfo = new ParkInfo(); for (int i = 0; i < jsonArr.size(); i++) { JSONObject job = jsonArr.getJSONObject(i); String parkCode = job.getString("parkCode"); String streetName = job.getString("streetName"); int isInput = job.getInt("isInput"); String parkName = job.getString("parkName"); String longitude = job.getString("longitude"); String latitude = job.getString("latitude"); if ("0".equals(latitude) && "0".equals(longitude)) { continue; } int berthNum = job.getInt("berthNum"); int berthRemain = job.getInt("berthRemain"); int parkingCount = JSONNull.class==job.get("parkingCount").getClass() ? 0: job.getInt("parkingCount"); String receivableAmount = job.getString("receivableAmount"); String realPaidAmount = job.getString("realPaidAmount"); int parkModel = job.getInt("parkModel"); parkInfo.setParkcode(parkCode); parkInfo.setStreetname(streetName); parkInfo.setIsinput(isInput); parkInfo.setParkname(parkName); parkInfo.setLongitude(longitude); parkInfo.setLatitude(latitude); parkInfo.setBerthnum(berthNum); parkInfo.setBerthremain(berthRemain); parkInfo.setParkingcount(parkingCount); parkInfo.setReceivableamount(receivableAmount); parkInfo.setRealpaidamount(realPaidAmount); parkInfo.setParkmodel(parkModel); ParkInfo parkInfo1 = parkInfoMapper.findByNo(parkInfo.getParkcode()); if (parkInfo1 != null) { ParkInfo parkInfo2 = parkInfoMapper.findByid(parkInfo1.getId()); parkInfoMapper.insertside(parkInfo2); parkInfoMapper.updateByPrimaryKeySelective(parkInfo); // System.out.println("数据5方法已运行"); } else { parkInfoMapper.insertSelective(parkInfo); } } } }