这是我第一次在it邦发文章,主要是做一下笔记,顺便和大家技术交流,请各位指教
首先是IFTTT,说明请参照IFTTT 发送 LINE 讯息通知
再来是java的类别部分
JMaker.javapackage javaapplication4;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.List;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.HttpClientBuilder;public class JMaker { private String eventName; private String key; public JMaker(String eventName, String key) { this.eventName = eventName; this.key = key; } public void trigger() throws IOException { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key); httpClient.execute(request); } public void trigger(String values) throws IOException { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key); StringEntity params = new StringEntity(buildJson(values), "UTF-8"); request.addHeader("content-type", "application/json"); request.setEntity(params); httpClient.execute(request); } //2018-07-25 确认是否发送 public void trigger(String values, int state) throws IOException { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key); if (state == 0) { StringEntity params = new StringEntity(buildJson(values), "UTF-8"); request.addHeader("content-type", "application/json"); request.setEntity(params); httpClient.execute(request); } else { httpClient.execute(request); } } private String buildJson(String values) throws UnsupportedEncodingException { String json = "{"; json += "\"value1\":\"" + values + "\""; json += "}"; System.out.println("传送 json: " + json); return json; }}
最后是使用的部分
main.javaJMaker maker = new JMaker(IFTTT的eventNam, IFTTT的key);maker.trigger("test");
lib引用的部分
完成的样子
最后再次讚叹google大神的伟大,也感谢各位大大无私的分享,在此贡献小弟一点经验,回馈大家