【发布时间】:2019-07-04 09:20:47
【问题描述】:
我正在尝试使用 Slack 传入 webhook 发送消息。我有以下代码。它运行,但是当我检查我的松弛时,没有消息。谁能看到我可能做错了什么。
public class SlackTest {
static String web_hook_url = "https://hooks.slack.com/services/***********/******************";
public static void main(String[] args) {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(web_hook_url);
try {
String json = "{\"name\": John}";
System.out.println(json);
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
client.execute(httpPost);
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
我认为你的错误处理不足以看到 API 错误。
IOException只会在出现低级错误时引发(例如,没有连接到服务器)。看这里stackoverflow.com/questions/32434947/… -
您想要获取 API 的响应并检查它是否有错误,例如
HttpResponse response = client.execute(httpPost); -
查看此答案以了解如何阅读回复:stackoverflow.com/questions/14024625/…
-
我真的想通了。不过谢谢!
-
太棒了。愿意将解决方案发布为答案吗?