-
Notifications
You must be signed in to change notification settings - Fork 24
/
ServerChanUtil.java
68 lines (53 loc) · 1.72 KB
/
ServerChanUtil.java
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
package me.rowkey.libs.util;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.List;
/**
* Author: Bryant Hang
* Date: 15/9/22
* Time: 14:50
*/
public class ServerChanUtil {
private static HttpClientUtil httpClientUtil = null;
private static final String SEND_URL = "http://sc.ftqq.com/%s.send";
private String sckey;
public ServerChanUtil(String sckey) {
Preconditions.checkArgument(StringUtils.isNotBlank(sckey));
this.sckey = sckey;
}
public boolean sendMsg(String text, String desp) throws IOException {
if (httpClientUtil == null) {
synchronized (ServerChanUtil.class) {
if (httpClientUtil == null) {
httpClientUtil = new HttpClientUtil();
}
}
}
String url = String.format(SEND_URL, sckey);
List<NameValuePair> params = Lists.newArrayList();
params.add(new BasicNameValuePair("text", text));
params.add(new BasicNameValuePair("desp", desp));
String result = httpClientUtil.postData(url, params);
if (StringUtils.isBlank(result)) {
return false;
}
JSONObject jo = null;
int errno = -1;
try {
jo = new JSONObject(result);
errno = jo.getInt("errno");
} catch (JSONException e) {
return false;
}
if (jo == null) {
return false;
}
return errno == 0;
}
}