正在显示
11 个修改的文件
包含
670 行增加
和
101 行删除
1 | -package com.sunyo.energy.location.controller; | ||
2 | - | ||
3 | - | ||
4 | -import com.github.pagehelper.Page; | ||
5 | -import com.github.pagehelper.PageHelper; | ||
6 | -import com.github.pagehelper.PageInfo; | ||
7 | -import com.sunyo.energy.location.controller.response.ResultJson; | ||
8 | -import com.sunyo.energy.location.dao.LocationMapper; | ||
9 | -import com.sunyo.energy.location.model.Location; | ||
10 | -import org.springframework.beans.factory.annotation.Autowired; | ||
11 | -import org.springframework.web.bind.annotation.RequestMapping; | ||
12 | -import org.springframework.web.bind.annotation.RequestMethod; | ||
13 | -import org.springframework.web.bind.annotation.RequestParam; | ||
14 | -import org.springframework.web.bind.annotation.RestController; | ||
15 | - | ||
16 | -import java.util.List; | ||
17 | - | ||
18 | -@RestController | ||
19 | -@RequestMapping("/location") | ||
20 | -public class LocationController { | ||
21 | - | ||
22 | - @Autowired | ||
23 | - private LocationMapper locationMapper; | ||
24 | - | ||
25 | - @RequestMapping(value="/list",method= RequestMethod.GET) | ||
26 | - public ResultJson startActivityDemo(@RequestParam(value = "pageNum",required = false,defaultValue = "1") | ||
27 | - int pageNum, | ||
28 | - @RequestParam(value = "pageSize",required = false,defaultValue = "5") | ||
29 | - int pageSize){ | ||
30 | - Page<Location> page = PageHelper.startPage(pageNum,pageSize); | ||
31 | - List<Location> list= locationMapper.selectAll(0); | ||
32 | - PageInfo<Location> result = new PageInfo<Location>(list); | ||
33 | - return new ResultJson("200","success",result); | ||
34 | - } | ||
35 | -} |
1 | +package com.sunyo.energy.location.controller; | ||
2 | + | ||
3 | +import com.sunyo.energy.location.controller.response.ResultJson; | ||
4 | +import com.sunyo.energy.location.model.WaterMeter; | ||
5 | +import com.sunyo.energy.location.service.WaterMeterService; | ||
6 | +import org.springframework.beans.factory.annotation.Autowired; | ||
7 | +import org.springframework.stereotype.Controller; | ||
8 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
9 | +import org.springframework.web.bind.annotation.RequestParam; | ||
10 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
11 | + | ||
12 | +@Controller | ||
13 | +@RequestMapping("/water_meter") | ||
14 | +public class WaterMeterController { | ||
15 | + | ||
16 | + @Autowired | ||
17 | + private WaterMeterService waterMeterService; | ||
18 | + | ||
19 | + /** | ||
20 | + * 水表实施信息入库 | ||
21 | + * @param resultJson | ||
22 | + * @return | ||
23 | + */ | ||
24 | + @RequestMapping("/realTime") | ||
25 | + @ResponseBody | ||
26 | + public ResultJson realTime(ResultJson resultJson){ | ||
27 | + int i = waterMeterService.realTime(); | ||
28 | + if (i>0){ | ||
29 | + resultJson.setCode("200"); | ||
30 | + resultJson.setMsg("操作成功"); | ||
31 | + }else { | ||
32 | + resultJson.setCode("500"); | ||
33 | + resultJson.setMsg("操作失败"); | ||
34 | + } | ||
35 | + | ||
36 | + return resultJson; | ||
37 | + } | ||
38 | + | ||
39 | + /** | ||
40 | + * 水表单个实施信息查询 | ||
41 | + */ | ||
42 | + @RequestMapping("/findRealTime") | ||
43 | + @ResponseBody | ||
44 | + public WaterMeter findRealTime(@RequestParam(value = "wmId", required = false) String wmId){ | ||
45 | + | ||
46 | + return waterMeterService.findRealTime(wmId); | ||
47 | + } | ||
48 | + | ||
49 | +} |
1 | -package com.sunyo.energy.location.dao; | ||
2 | - | ||
3 | -import com.sunyo.energy.location.model.Location; | ||
4 | - | ||
5 | -import java.util.List; | ||
6 | - | ||
7 | -public interface LocationMapper { | ||
8 | - int deleteByPrimaryKey(Integer id); | ||
9 | - | ||
10 | - int insert(Location record); | ||
11 | - | ||
12 | - int insertSelective(Location record); | ||
13 | - | ||
14 | - List<Location> selectByPrimaryKey(Integer id); | ||
15 | - | ||
16 | - List<Location> selectAll(Integer id); | ||
17 | - | ||
18 | - int updateByPrimaryKeySelective(Location record); | ||
19 | - | ||
20 | - int updateByPrimaryKey(Location record); | ||
21 | -} |
1 | +package com.sunyo.energy.location.dao; | ||
2 | + | ||
3 | +import com.sunyo.energy.location.model.WaterElectricityParameter; | ||
4 | + | ||
5 | +public interface WaterElectricityParameterMapper { | ||
6 | + int deleteByPrimaryKey(Integer id); | ||
7 | + | ||
8 | + int insert(WaterElectricityParameter record); | ||
9 | + | ||
10 | + int insertSelective(WaterElectricityParameter record); | ||
11 | + | ||
12 | + WaterElectricityParameter selectByPrimaryKey(Integer id); | ||
13 | + | ||
14 | + int updateByPrimaryKeySelective(WaterElectricityParameter record); | ||
15 | + | ||
16 | + int updateByPrimaryKey(WaterElectricityParameter record); | ||
17 | +} |
1 | -package com.sunyo.energy.location.model; | ||
2 | - | ||
3 | -import lombok.Data; | ||
4 | - | ||
5 | -import java.util.List; | ||
6 | - | ||
7 | -@Data | ||
8 | -public class Location { | ||
9 | - private Integer id; | ||
10 | - | ||
11 | - private String adrname; | ||
12 | - | ||
13 | - private Integer parent; | ||
14 | - | ||
15 | - private Integer type; | ||
16 | - | ||
17 | - private List<Location> children; | ||
18 | - | ||
19 | - public Integer getId() { | ||
20 | - return id; | ||
21 | - } | ||
22 | - | ||
23 | - public void setId(Integer id) { | ||
24 | - this.id = id; | ||
25 | - } | ||
26 | - | ||
27 | - public String getAdrname() { | ||
28 | - return adrname; | ||
29 | - } | ||
30 | - | ||
31 | - public void setAdrname(String adrname) { | ||
32 | - this.adrname = adrname == null ? null : adrname.trim(); | ||
33 | - } | ||
34 | - | ||
35 | - public Integer getParent() { | ||
36 | - return parent; | ||
37 | - } | ||
38 | - | ||
39 | - public void setParent(Integer parent) { | ||
40 | - this.parent = parent; | ||
41 | - } | ||
42 | - | ||
43 | - | ||
44 | - | ||
45 | -} |
1 | +package com.sunyo.energy.location.model; | ||
2 | + | ||
3 | +import java.util.Date; | ||
4 | + | ||
5 | +public class WaterElectricityParameter { | ||
6 | + private Integer id; | ||
7 | + | ||
8 | + private Boolean prepaid; | ||
9 | + | ||
10 | + private Boolean warningtrip; | ||
11 | + | ||
12 | + private Long warningthreshold; | ||
13 | + | ||
14 | + private Boolean overdraft; | ||
15 | + | ||
16 | + private Long overdraftthreshold; | ||
17 | + | ||
18 | + private Boolean load; | ||
19 | + | ||
20 | + private Long water; | ||
21 | + | ||
22 | + private Long power; | ||
23 | + | ||
24 | + private String powerfactor; | ||
25 | + | ||
26 | + private Boolean free; | ||
27 | + | ||
28 | + private Long freeelectricitylimit; | ||
29 | + | ||
30 | + private String freewater; | ||
31 | + | ||
32 | + private String wmId; | ||
33 | + | ||
34 | + private Date creattime; | ||
35 | + | ||
36 | + private String eeId; | ||
37 | + | ||
38 | + private String reamke2; | ||
39 | + | ||
40 | + private String reamke3; | ||
41 | + | ||
42 | + public Integer getId() { | ||
43 | + return id; | ||
44 | + } | ||
45 | + | ||
46 | + public void setId(Integer id) { | ||
47 | + this.id = id; | ||
48 | + } | ||
49 | + | ||
50 | + public Boolean getPrepaid() { | ||
51 | + return prepaid; | ||
52 | + } | ||
53 | + | ||
54 | + public void setPrepaid(Boolean prepaid) { | ||
55 | + this.prepaid = prepaid; | ||
56 | + } | ||
57 | + | ||
58 | + public Boolean getWarningtrip() { | ||
59 | + return warningtrip; | ||
60 | + } | ||
61 | + | ||
62 | + public void setWarningtrip(Boolean warningtrip) { | ||
63 | + this.warningtrip = warningtrip; | ||
64 | + } | ||
65 | + | ||
66 | + public Long getWarningthreshold() { | ||
67 | + return warningthreshold; | ||
68 | + } | ||
69 | + | ||
70 | + public void setWarningthreshold(Long warningthreshold) { | ||
71 | + this.warningthreshold = warningthreshold; | ||
72 | + } | ||
73 | + | ||
74 | + public Boolean getOverdraft() { | ||
75 | + return overdraft; | ||
76 | + } | ||
77 | + | ||
78 | + public void setOverdraft(Boolean overdraft) { | ||
79 | + this.overdraft = overdraft; | ||
80 | + } | ||
81 | + | ||
82 | + public Long getOverdraftthreshold() { | ||
83 | + return overdraftthreshold; | ||
84 | + } | ||
85 | + | ||
86 | + public void setOverdraftthreshold(Long overdraftthreshold) { | ||
87 | + this.overdraftthreshold = overdraftthreshold; | ||
88 | + } | ||
89 | + | ||
90 | + public Boolean getLoad() { | ||
91 | + return load; | ||
92 | + } | ||
93 | + | ||
94 | + public void setLoad(Boolean load) { | ||
95 | + this.load = load; | ||
96 | + } | ||
97 | + | ||
98 | + public Long getWater() { | ||
99 | + return water; | ||
100 | + } | ||
101 | + | ||
102 | + public void setWater(Long water) { | ||
103 | + this.water = water; | ||
104 | + } | ||
105 | + | ||
106 | + public Long getPower() { | ||
107 | + return power; | ||
108 | + } | ||
109 | + | ||
110 | + public void setPower(Long power) { | ||
111 | + this.power = power; | ||
112 | + } | ||
113 | + | ||
114 | + public String getPowerfactor() { | ||
115 | + return powerfactor; | ||
116 | + } | ||
117 | + | ||
118 | + public void setPowerfactor(String powerfactor) { | ||
119 | + this.powerfactor = powerfactor == null ? null : powerfactor.trim(); | ||
120 | + } | ||
121 | + | ||
122 | + public Boolean getFree() { | ||
123 | + return free; | ||
124 | + } | ||
125 | + | ||
126 | + public void setFree(Boolean free) { | ||
127 | + this.free = free; | ||
128 | + } | ||
129 | + | ||
130 | + public Long getFreeelectricitylimit() { | ||
131 | + return freeelectricitylimit; | ||
132 | + } | ||
133 | + | ||
134 | + public void setFreeelectricitylimit(Long freeelectricitylimit) { | ||
135 | + this.freeelectricitylimit = freeelectricitylimit; | ||
136 | + } | ||
137 | + | ||
138 | + public String getFreewater() { | ||
139 | + return freewater; | ||
140 | + } | ||
141 | + | ||
142 | + public void setFreewater(String freewater) { | ||
143 | + this.freewater = freewater == null ? null : freewater.trim(); | ||
144 | + } | ||
145 | + | ||
146 | + public String getWmId() { | ||
147 | + return wmId; | ||
148 | + } | ||
149 | + | ||
150 | + public void setWmId(String wmId) { | ||
151 | + this.wmId = wmId == null ? null : wmId.trim(); | ||
152 | + } | ||
153 | + | ||
154 | + public Date getCreattime() { | ||
155 | + return creattime; | ||
156 | + } | ||
157 | + | ||
158 | + public void setCreattime(Date creattime) { | ||
159 | + this.creattime = creattime; | ||
160 | + } | ||
161 | + | ||
162 | + public String getEeId() { | ||
163 | + return eeId; | ||
164 | + } | ||
165 | + | ||
166 | + public void setEeId(String eeId) { | ||
167 | + this.eeId = eeId == null ? null : eeId.trim(); | ||
168 | + } | ||
169 | + | ||
170 | + public String getReamke2() { | ||
171 | + return reamke2; | ||
172 | + } | ||
173 | + | ||
174 | + public void setReamke2(String reamke2) { | ||
175 | + this.reamke2 = reamke2 == null ? null : reamke2.trim(); | ||
176 | + } | ||
177 | + | ||
178 | + public String getReamke3() { | ||
179 | + return reamke3; | ||
180 | + } | ||
181 | + | ||
182 | + public void setReamke3(String reamke3) { | ||
183 | + this.reamke3 = reamke3 == null ? null : reamke3.trim(); | ||
184 | + } | ||
185 | +} |
1 | +package com.sunyo.energy.location.model; | ||
2 | + | ||
3 | +import java.util.Date; | ||
4 | + | ||
5 | +public class WaterMeter { | ||
6 | + private String wmId; | ||
7 | + | ||
8 | + private String wmSacc; | ||
9 | + | ||
10 | + private String wmLacc; | ||
11 | + | ||
12 | + private String wmRdtime; | ||
13 | + | ||
14 | + private String wmFmstate; | ||
15 | + | ||
16 | + private String wmErrmessage; | ||
17 | + | ||
18 | + private String wmVoltage; | ||
19 | + | ||
20 | + private String wmSignalpower; | ||
21 | + | ||
22 | + private Date creattime; | ||
23 | + | ||
24 | + private Date updatetime; | ||
25 | + | ||
26 | + private String reamke1; | ||
27 | + | ||
28 | + private String reamke2; | ||
29 | + | ||
30 | + private String reamke3; | ||
31 | + | ||
32 | + private String reamke4; | ||
33 | + | ||
34 | + public String getWmId() { | ||
35 | + return wmId; | ||
36 | + } | ||
37 | + | ||
38 | + public void setWmId(String wmId) { | ||
39 | + this.wmId = wmId == null ? null : wmId.trim(); | ||
40 | + } | ||
41 | + | ||
42 | + public String getWmSacc() { | ||
43 | + return wmSacc; | ||
44 | + } | ||
45 | + | ||
46 | + public void setWmSacc(String wmSacc) { | ||
47 | + this.wmSacc = wmSacc == null ? null : wmSacc.trim(); | ||
48 | + } | ||
49 | + | ||
50 | + public String getWmLacc() { | ||
51 | + return wmLacc; | ||
52 | + } | ||
53 | + | ||
54 | + public void setWmLacc(String wmLacc) { | ||
55 | + this.wmLacc = wmLacc == null ? null : wmLacc.trim(); | ||
56 | + } | ||
57 | + | ||
58 | + public String getWmRdtime() { | ||
59 | + return wmRdtime; | ||
60 | + } | ||
61 | + | ||
62 | + public void setWmRdtime(String wmRdtime) { | ||
63 | + this.wmRdtime = wmRdtime == null ? null : wmRdtime.trim(); | ||
64 | + } | ||
65 | + | ||
66 | + public String getWmFmstate() { | ||
67 | + return wmFmstate; | ||
68 | + } | ||
69 | + | ||
70 | + public void setWmFmstate(String wmFmstate) { | ||
71 | + this.wmFmstate = wmFmstate == null ? null : wmFmstate.trim(); | ||
72 | + } | ||
73 | + | ||
74 | + public String getWmErrmessage() { | ||
75 | + return wmErrmessage; | ||
76 | + } | ||
77 | + | ||
78 | + public void setWmErrmessage(String wmErrmessage) { | ||
79 | + this.wmErrmessage = wmErrmessage == null ? null : wmErrmessage.trim(); | ||
80 | + } | ||
81 | + | ||
82 | + public String getWmVoltage() { | ||
83 | + return wmVoltage; | ||
84 | + } | ||
85 | + | ||
86 | + public void setWmVoltage(String wmVoltage) { | ||
87 | + this.wmVoltage = wmVoltage == null ? null : wmVoltage.trim(); | ||
88 | + } | ||
89 | + | ||
90 | + public String getWmSignalpower() { | ||
91 | + return wmSignalpower; | ||
92 | + } | ||
93 | + | ||
94 | + public void setWmSignalpower(String wmSignalpower) { | ||
95 | + this.wmSignalpower = wmSignalpower == null ? null : wmSignalpower.trim(); | ||
96 | + } | ||
97 | + | ||
98 | + public Date getCreattime() { | ||
99 | + return creattime; | ||
100 | + } | ||
101 | + | ||
102 | + public void setCreattime(Date creattime) { | ||
103 | + this.creattime = creattime; | ||
104 | + } | ||
105 | + | ||
106 | + public Date getUpdatetime() { | ||
107 | + return updatetime; | ||
108 | + } | ||
109 | + | ||
110 | + public void setUpdatetime(Date updatetime) { | ||
111 | + this.updatetime = updatetime; | ||
112 | + } | ||
113 | + | ||
114 | + public String getReamke1() { | ||
115 | + return reamke1; | ||
116 | + } | ||
117 | + | ||
118 | + public void setReamke1(String reamke1) { | ||
119 | + this.reamke1 = reamke1 == null ? null : reamke1.trim(); | ||
120 | + } | ||
121 | + | ||
122 | + public String getReamke2() { | ||
123 | + return reamke2; | ||
124 | + } | ||
125 | + | ||
126 | + public void setReamke2(String reamke2) { | ||
127 | + this.reamke2 = reamke2 == null ? null : reamke2.trim(); | ||
128 | + } | ||
129 | + | ||
130 | + public String getReamke3() { | ||
131 | + return reamke3; | ||
132 | + } | ||
133 | + | ||
134 | + public void setReamke3(String reamke3) { | ||
135 | + this.reamke3 = reamke3 == null ? null : reamke3.trim(); | ||
136 | + } | ||
137 | + | ||
138 | + public String getReamke4() { | ||
139 | + return reamke4; | ||
140 | + } | ||
141 | + | ||
142 | + public void setReamke4(String reamke4) { | ||
143 | + this.reamke4 = reamke4 == null ? null : reamke4.trim(); | ||
144 | + } | ||
145 | +} |
1 | +package com.sunyo.energy.location.service.imp; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONArray; | ||
5 | +import com.sunyo.energy.location.dao.WaterMeterMapper; | ||
6 | +import com.sunyo.energy.location.model.WaterMeter; | ||
7 | +import com.sunyo.energy.location.service.WaterMeterService; | ||
8 | +import org.springframework.beans.factory.annotation.Autowired; | ||
9 | +import org.springframework.stereotype.Service; | ||
10 | +import utils.HttpsUtils; | ||
11 | + | ||
12 | +import java.util.*; | ||
13 | + | ||
14 | +@Service | ||
15 | +public class WaterMeterServiceImp implements WaterMeterService { | ||
16 | + | ||
17 | + @Autowired | ||
18 | + private WaterMeterMapper waterMeterMapper; | ||
19 | + | ||
20 | + | ||
21 | + @Override | ||
22 | + public int realTime() { | ||
23 | + | ||
24 | + try { | ||
25 | + // 调用远程接口 | ||
26 | + String url = "http://123.56.159.203:8023/nowwmrd/getSelectNowwmrdbyProjectID"; | ||
27 | + Map<String, Object> datas = new HashMap<>(); | ||
28 | + datas.put("ProjectID", "33-99-00-00-00-00-01"); | ||
29 | + | ||
30 | + String s = HttpsUtils.httpRequest(url, datas); | ||
31 | + List<WaterMeter> waterMeterList = new ArrayList<>(); | ||
32 | + JSONArray jsonArray = JSON.parseArray(s); | ||
33 | + for (Object jsonObject:jsonArray){ | ||
34 | + WaterMeter waterMeter= new WaterMeter(); | ||
35 | + Map<String,Object> map = (Map)jsonObject; | ||
36 | + if (!map.containsKey("state")){ | ||
37 | + waterMeter.setWmId(map.get("wm_id").toString()); | ||
38 | + waterMeter.setWmErrmessage(map.get("wm_errmessage").toString()); | ||
39 | + waterMeter.setWmFmstate(map.get("wm_fmstate").toString()); | ||
40 | + waterMeter.setWmSacc(map.get("wm_sacc").toString()); | ||
41 | + waterMeter.setWmLacc(map.get("wm_lacc").toString()); | ||
42 | + waterMeter.setWmRdtime(map.get("wm_rdtime").toString()); | ||
43 | + waterMeter.setWmSignalpower(map.get("wm_signalpower").toString()); | ||
44 | + waterMeter.setWmVoltage(map.get("wm_voltage").toString()); | ||
45 | + waterMeter.setCreattime(new Date()); | ||
46 | + waterMeterMapper.insertSelective(waterMeter); | ||
47 | + }else { | ||
48 | + if ("0".equals(map.get("state").toString())){ | ||
49 | + return 1; | ||
50 | + } | ||
51 | + return 0; | ||
52 | + } | ||
53 | + } | ||
54 | + return 1; | ||
55 | + }catch (Exception e){ | ||
56 | + e.printStackTrace(); | ||
57 | + return 0; | ||
58 | + } | ||
59 | + } | ||
60 | + | ||
61 | + @Override | ||
62 | + public WaterMeter findRealTime(String wmId) { | ||
63 | + try { | ||
64 | + // 调用远程接口http://123.56.159.203:8023/nowwmrd/getSelectNowwmrdbyMtId?MtId=68-74-40-34-05-29-55 | ||
65 | + String url = "http://123.56.159.203:8023/nowwmrd/getSelectNowwmrdbyMtId"; | ||
66 | + Map<String, Object> datas = new HashMap<>(); | ||
67 | + datas.put("MtId", wmId); | ||
68 | + | ||
69 | + String s = HttpsUtils.httpRequest(url, datas); | ||
70 | + JSONArray jsonArray = JSON.parseArray(s); | ||
71 | + WaterMeter waterMeter= new WaterMeter(); | ||
72 | + for (Object jsonObject:jsonArray){ | ||
73 | + | ||
74 | + Map<String,Object> map = (Map)jsonObject; | ||
75 | + if (!map.containsKey("state")){ | ||
76 | + waterMeter.setWmId(map.get("wm_id").toString()); | ||
77 | + waterMeter.setWmErrmessage(map.get("wm_errmessage").toString()); | ||
78 | + waterMeter.setWmFmstate(map.get("wm_fmstate").toString()); | ||
79 | + waterMeter.setWmSacc(map.get("wm_sacc").toString()); | ||
80 | + waterMeter.setWmLacc(map.get("wm_lacc").toString()); | ||
81 | + waterMeter.setWmRdtime(map.get("wm_rdtime").toString()); | ||
82 | + waterMeter.setWmSignalpower(map.get("wm_signalpower").toString()); | ||
83 | + waterMeter.setWmVoltage(map.get("wm_voltage").toString()); | ||
84 | + } | ||
85 | + } | ||
86 | + return waterMeter; | ||
87 | + }catch (Exception e){ | ||
88 | + e.printStackTrace(); | ||
89 | + return new WaterMeter(); | ||
90 | + } | ||
91 | + } | ||
92 | +} |
src/main/java/utils/HttpsUtils.java
0 → 100644
1 | +package utils; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONArray; | ||
5 | +import com.alibaba.fastjson.JSONObject; | ||
6 | +import com.sunyo.energy.location.model.WaterMeter; | ||
7 | +import jdk.nashorn.internal.parser.JSONParser; | ||
8 | + | ||
9 | +import java.io.*; | ||
10 | +import java.net.HttpURLConnection; | ||
11 | +import java.net.URL; | ||
12 | +import java.net.URLConnection; | ||
13 | +import java.net.URLEncoder; | ||
14 | +import java.util.ArrayList; | ||
15 | +import java.util.HashMap; | ||
16 | +import java.util.List; | ||
17 | +import java.util.Map; | ||
18 | + | ||
19 | +public class HttpsUtils { | ||
20 | + | ||
21 | + /** | ||
22 | + * java调用运程api公共方法 | ||
23 | + * | ||
24 | + * @param requestUrl | ||
25 | + * @param params | ||
26 | + * @return | ||
27 | + */ | ||
28 | + @SuppressWarnings({"unchecked", "rawtypes"}) | ||
29 | + public static String httpRequest(String requestUrl, Map params) throws Exception { | ||
30 | + // buffer用于接受返回的字符 | ||
31 | + StringBuffer buffer = new StringBuffer(); | ||
32 | + | ||
33 | + // 建立URL,把请求地址给补全,其中urlencode()方法用于把params里的参数给取出来 | ||
34 | + URL url = new URL(requestUrl + "?" + urlencode(params)); | ||
35 | + // 打开http连接 | ||
36 | + HttpURLConnection httpUrlConn = (HttpURLConnection) url | ||
37 | + .openConnection(); | ||
38 | + httpUrlConn.setDoInput(true); | ||
39 | + httpUrlConn.setRequestMethod("GET"); | ||
40 | + httpUrlConn.connect(); | ||
41 | + | ||
42 | + // 获得输入 | ||
43 | + InputStream inputStream = httpUrlConn.getInputStream(); | ||
44 | + InputStreamReader inputStreamReader = new InputStreamReader( | ||
45 | + inputStream, "utf-8"); | ||
46 | + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); | ||
47 | + | ||
48 | + // 将bufferReader的值给放到buffer里 | ||
49 | + String str = null; | ||
50 | + while ((str = bufferedReader.readLine()) != null) { | ||
51 | + buffer.append(str); | ||
52 | + } | ||
53 | + // 关闭bufferReader和输入流 | ||
54 | + bufferedReader.close(); | ||
55 | + inputStreamReader.close(); | ||
56 | + inputStream.close(); | ||
57 | + inputStream = null; | ||
58 | + // 断开连接 | ||
59 | + httpUrlConn.disconnect(); | ||
60 | + | ||
61 | + // 返回字符串 | ||
62 | + return buffer.toString(); | ||
63 | + } | ||
64 | + | ||
65 | + /** | ||
66 | + * 请求参数拼接(组装) | ||
67 | + * | ||
68 | + * @param data | ||
69 | + * @return | ||
70 | + */ | ||
71 | + @SuppressWarnings("rawtypes") | ||
72 | + public static String urlencode(Map<String, Object> data) { | ||
73 | + // 将map里的参数变成像 showapi_appid=###&showapi_sign=###&的样子 | ||
74 | + StringBuilder sb = new StringBuilder(); | ||
75 | + for (Map.Entry i : data.entrySet()) { | ||
76 | + try { | ||
77 | + sb.append(i.getKey()).append("=") | ||
78 | + .append(URLEncoder.encode(i.getValue() + "", "UTF-8")) | ||
79 | + .append("&"); | ||
80 | + } catch (UnsupportedEncodingException e) { | ||
81 | + e.printStackTrace(); | ||
82 | + } | ||
83 | + } | ||
84 | + return sb.toString(); | ||
85 | + } | ||
86 | + | ||
87 | + /** | ||
88 | + * 向指定 URL 发送POST方法的请求 | ||
89 | + * | ||
90 | + * @param url 发送请求的 URL | ||
91 | + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 | ||
92 | + * @return 所代表远程资源的响应结果 | ||
93 | + */ | ||
94 | + public static String sendPostHttpRequest(String url, String param) { | ||
95 | + PrintWriter out = null; | ||
96 | + BufferedReader in = null; | ||
97 | + String result = ""; | ||
98 | + try { | ||
99 | + URL realUrl = new URL(url); | ||
100 | + // 打开和URL之间的连接 | ||
101 | + URLConnection conn = realUrl.openConnection(); | ||
102 | + // 设置通用的请求属性 | ||
103 | + conn.setRequestProperty("accept", "*/*"); | ||
104 | + conn.setRequestProperty("connection", "Keep-Alive"); | ||
105 | + conn.setRequestProperty("user-agent", | ||
106 | + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | ||
107 | + // 发送POST请求必须设置如下两行 | ||
108 | + conn.setDoOutput(true); | ||
109 | + conn.setDoInput(true); | ||
110 | + // 1.获取URLConnection对象对应的输出流 | ||
111 | + out = new PrintWriter(conn.getOutputStream()); | ||
112 | + // 2.中文有乱码的需要将PrintWriter改为如下 | ||
113 | + // out=new OutputStreamWriter(conn.getOutputStream(),"UTF-8") | ||
114 | + // 发送请求参数 | ||
115 | + out.print(param); | ||
116 | + // flush输出流的缓冲 | ||
117 | + out.flush(); | ||
118 | + // 定义BufferedReader输入流来读取URL的响应 | ||
119 | + in = new BufferedReader( | ||
120 | + new InputStreamReader(conn.getInputStream())); | ||
121 | + String line; | ||
122 | + while ((line = in.readLine()) != null) { | ||
123 | + result += line; | ||
124 | + } | ||
125 | + } catch (Exception e) { | ||
126 | + System.out.println("发送 POST 请求出现异常!" + e); | ||
127 | + e.printStackTrace(); | ||
128 | + } | ||
129 | + // 使用finally块来关闭输出流、输入流 | ||
130 | + finally { | ||
131 | + try { | ||
132 | + if (out != null) { | ||
133 | + out.close(); | ||
134 | + } | ||
135 | + if (in != null) { | ||
136 | + in.close(); | ||
137 | + } | ||
138 | + } catch (IOException ex) { | ||
139 | + ex.printStackTrace(); | ||
140 | + } | ||
141 | + } | ||
142 | + return result; | ||
143 | + } | ||
144 | + | ||
145 | + // 测试是否有效 | ||
146 | +// public static void main(String[] args) { | ||
147 | +// // 接口地址 | ||
148 | +// String requestUrl = "http://123.56.159.203:8023/mtfmset/allWMadd"; | ||
149 | +// try { | ||
150 | +// | ||
151 | +// String url = "http://123.56.159.203:8023/nowwmrd/getSelectNowwmrdbyProjectID"; | ||
152 | +// Map<String, Object> datas = new HashMap<>(); | ||
153 | +// datas.put("ProjectID", "33-99-00-00-00-00-01"); | ||
154 | +// List list = new ArrayList<>(); | ||
155 | +// | ||
156 | +// } catch (Exception e) { | ||
157 | +// e.printStackTrace(); | ||
158 | +// } | ||
159 | +// } | ||
160 | +} |
-
请 注册 或 登录 后发表评论