1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package builder;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class httpApi {
 
    public static String Ip="localhost";
    public static void main(String[] args) {
 
 
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("thickness", "5");
        jsonObject.put("width", "3660");
        jsonObject.put("height", "2440");
        jsonObject.put("sameCount", "2");
        jsonObject.put("glassType", "超白玻");
        sendShelfTask(jsonObject);
        JSONObject a=selectLoadRack();
        double width=Double.valueOf(a.get("width").toString());
        double height=Double.valueOf(a.get("height").toString());
        double thickness=Double.valueOf(a.get("thickness").toString());
        int sameCount=Integer.valueOf(a.get("sameCount").toString());
        System.out.println(width+","+height+","+thickness+","+sameCount);
 
    }
    //发送仓储所需玻璃任务
    public static JSONObject sendShelfTask(JSONObject params){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo";
            String requestBody = params.toString();
            JSONObject resultJSON= httpApi(requestURL,"POST",requestBody);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo  POST");
        }
        return null;
    }
    //MES是否正常连接
    public static JSONObject sendShelfTask(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo  GET");
        }
        return null;
    }
    //查询上片位信息
    public static JSONObject selectLoadRack(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo/getReady";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            JSONObject loadRack=resultJSON.getJSONObject("data").getJSONArray("stocklist").getJSONObject(0);
            return loadRack;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://"+Ip+":5000/GlassInfo GET");
        }
        return null;
    }
    //反馈上片位信息-1
    public static JSONObject loadRackReduction(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo/finishGet";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://"+Ip+":5000/GlassInfo/finishGet GET");
        }
        return null;
    }
    //查询仓储库内信息
    public static JSONObject storageRackInfo(){
        try {
            String requestURL="http://"+Ip+":5000/GlassInfo";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo GET");
        }
        return null;
    }
    public static JSONObject httpApi(String requestURL,String requestMethod,String requestBody){
        JSONObject jsonObject = null;
        try {
            // 地址 URL
            URL url = new URL(requestURL);
            // 连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 请求方式
            connection.setRequestMethod(requestMethod);
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-type", "application/json; charset=UTF-8");
            System.out.println(requestBody);
            // 参数写入
            if(requestBody!=null){
                try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                    outputStream.writeBytes(requestBody);
                    outputStream.flush();
                }
            }
            // 状态码
            int responseCode = connection.getResponseCode();
            // 读取返回内容
            if (responseCode==200){
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                // 关闭
                connection.disconnect();
                String resultStr = response.toString();
                if (resultStr!=null&&resultStr.length()>0) {
                    jsonObject =JSONObject.parseObject(resultStr);
                }
                //System.out.println(jsonObject);
            }else{
                System.out.println("Response Code: " + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
 
}