API 方式集成 OneAlert 的 Java 源代码

java
onealert
sdk
api

#1

example

/**
 * Created by JerryLiu on 2018/12/12.
 */

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

public class Main {

        public static String postToAlert(JSONObject jsonParam) {
            StringBuffer sb=new StringBuffer();
            try {

                // 创建url资源
                URL url = new URL("http://api.onealert.com/alert/api/event/");
                // 建立http连接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
               // conn.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36");
                // 设置允许输出
                conn.setDoOutput(true);
                // 设置允许输入
                conn.setDoInput(true);
                // 设置不用缓存
                conn.setUseCaches(false);
                // 设置传递方式
                conn.setRequestMethod("POST");
                // 设置维持长连接
                conn.setRequestProperty("Connection", "Keep-Alive");
                // 设置文件字符集:
                //conn.setRequestProperty("Charset", "UTF-8");
                // 转换为字节数组
                byte[] data = (jsonParam.toString()).getBytes();
                // 设置文件长度
                conn.setRequestProperty("Content-Length", String.valueOf(data.length));
                // 设置文件类型:
                conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                // 开始连接请求
                conn.connect();
                OutputStream out = new DataOutputStream(conn.getOutputStream()) ;

                System.out.println((jsonParam.toString()));

                // 写入请求的字符串
                out.write((jsonParam.toString()).getBytes());
                out.flush();
                out.close();

                System.out.println(conn.getResponseCode());

                // 请求返回的状态
                if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
                    System.out.println("连接成功");
                    // 请求返回的数据
                    InputStream in1 = conn.getInputStream();
                    try {
                        String readLine=new String();
                        BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
                        while((readLine=responseReader.readLine())!=null){
                            sb.append(readLine).append("\n");
                        }
                        responseReader.close();
                        //System.out.println(sb.toString());

                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                } else {
                    System.out.println("error++");
                }

            } catch (Exception e) {
                e.printStackTrace();

            }

            return sb.toString();

        }


    public static void main(String[] args)  {

        JSONObject jsonParam = new JSONObject();

        jsonParam.put("app", "change-to-your-appkey"); // 登录 www.onealert.com,添加/查看应用获取 appkey
        jsonParam.put("eventType", "trigger");
        jsonParam.put("eventId", "12345");
        jsonParam.put("alarmContent", "aaa");
        jsonParam.put("entityName", "bbb");
        jsonParam.put("priority", 1);

        String data = postToAlert(jsonParam);

        System.out.println(data);

    }
}