Skip to content

Commit 8d2b57e

Browse files
committed
add log for api example
1 parent f8f2b3a commit 8d2b57e

File tree

3 files changed

+71
-39
lines changed

3 files changed

+71
-39
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,11 @@
258258
String res = client.registerUsers(users.toArray(regUsers));
259259
System.out.println(res);
260260
} catch (APIConnectionException e) {
261-
e.printStackTrace();
261+
LOG.error("Connection error. Should retry later. ", e);
262262
} catch (APIRequestException e) {
263-
System.out.println(e.getErrorMessage());
263+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
264+
LOG.info("HTTP Status: " + e.getStatus());
265+
LOG.info("Error Message: " + e.getMessage());
264266
}
265267
```
266268

@@ -274,9 +276,11 @@
274276
String res = client.createGroup("test_user", "test_gname1", "description", "test_user");
275277
System.out.println(res);
276278
} catch (APIConnectionException e) {
277-
System.out.println(e.getMessage());
279+
LOG.error("Connection error. Should retry later. ", e);
278280
} catch (APIRequestException e) {
279-
System.out.println(e.getErrorMessage());
281+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
282+
LOG.info("HTTP Status: " + e.getStatus());
283+
LOG.info("Error Message: " + e.getMessage());
280284
}
281285
```
282286

example/main/java/cn/jpush/api/examples/IMGroupExample.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
package cn.jpush.api.jmessage;
1+
package cn.jpush.api.examples;
22

3+
import cn.jpush.api.jmessage.JMessageClient;
34

45
import cn.jpush.api.common.resp.APIConnectionException;
56
import cn.jpush.api.jmessage.base.connection.APIRequestException;
67

78
public class IMGroupExample {
89

10+
protected static final Logger LOG = LoggerFactory.getLogger(IMGroupExample.class);
11+
912
private static final String appkey = "242780bfdd7315dc1989fe2b";
1013
private static final String masterSecret = "2f5ced2bef64167950e63d13";
1114

@@ -15,9 +18,11 @@ public static void testCreateGroup() {
1518
String res = client.createGroup("test_user", "test_gname1", "description", "test_user");
1619
System.out.println(res);
1720
} catch (APIConnectionException e) {
18-
System.out.println(e.getMessage());
21+
LOG.error("Connection error. Should retry later. ", e);
1922
} catch (APIRequestException e) {
20-
System.out.println(e.getErrorMessage());
23+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
24+
LOG.info("HTTP Status: " + e.getStatus());
25+
LOG.info("Error Message: " + e.getMessage());
2126
}
2227
}
2328

@@ -28,9 +33,11 @@ public static void testGetGroupInfo() {
2833
String res = client.getGroupInfo(10003767);
2934
System.out.println(res);
3035
} catch (APIConnectionException e) {
31-
System.out.println(e.getMessage());
36+
LOG.error("Connection error. Should retry later. ", e);
3237
} catch (APIRequestException e) {
33-
System.out.println(e.getErrorMessage());
38+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
39+
LOG.info("HTTP Status: " + e.getStatus());
40+
LOG.info("Error Message: " + e.getMessage());
3441
}
3542
}
3643

@@ -41,9 +48,11 @@ public static void testGetGroupMemberList() {
4148
String res = client.getGroupMembers(10003767);
4249
System.out.println(res);
4350
} catch (APIConnectionException e) {
44-
System.out.println(e.getMessage());
51+
LOG.error("Connection error. Should retry later. ", e);
4552
} catch (APIRequestException e) {
46-
System.out.println(e.getErrorMessage());
53+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
54+
LOG.info("HTTP Status: " + e.getStatus());
55+
LOG.info("Error Message: " + e.getMessage());
4756
}
4857
}
4958

@@ -54,9 +63,11 @@ public static void testGetGroupListByAppkey() {
5463
String res = client.getGroupListByAppkey(0, 30);
5564
System.out.println(res);
5665
} catch (APIConnectionException e) {
57-
System.out.println(e.getMessage());
66+
LOG.error("Connection error. Should retry later. ", e);
5867
} catch (APIRequestException e) {
59-
System.out.println(e.getErrorMessage());
68+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
69+
LOG.info("HTTP Status: " + e.getStatus());
70+
LOG.info("Error Message: " + e.getMessage());
6071
}
6172
}
6273

@@ -69,9 +80,11 @@ public static void testManageGroup() {
6980
client.addOrRemoveMembers(10003767, addList, null );
7081
client.addOrRemoveMembers(10003767, null, removeList);
7182
} catch (APIConnectionException e) {
72-
System.out.println(e.getMessage());
83+
LOG.error("Connection error. Should retry later. ", e);
7384
} catch (APIRequestException e) {
74-
System.out.println(e.getErrorMessage());
85+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
86+
LOG.info("HTTP Status: " + e.getStatus());
87+
LOG.info("Error Message: " + e.getMessage());
7588
}
7689
}
7790

@@ -81,9 +94,11 @@ public static void testUpdateGroupInfo() {
8194
try {
8295
client.updateGroupInfo(10003767, "test_gname_new", "update desc");
8396
} catch (APIConnectionException e) {
84-
System.out.println(e.getMessage());
97+
LOG.error("Connection error. Should retry later. ", e);
8598
} catch (APIRequestException e) {
86-
System.out.println(e.getErrorMessage());
99+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
100+
LOG.info("HTTP Status: " + e.getStatus());
101+
LOG.info("Error Message: " + e.getMessage());
87102
}
88103
}
89104

@@ -93,17 +108,15 @@ public static void testDeleteGroup() {
93108
try {
94109
client.deleteGroup(10003765);
95110
} catch (APIConnectionException e) {
96-
System.out.println(e.getMessage());
111+
LOG.error("Connection error. Should retry later. ", e);
97112
} catch (APIRequestException e) {
98-
System.out.println(e.getErrorMessage());
113+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
114+
LOG.info("HTTP Status: " + e.getStatus());
115+
LOG.info("Error Message: " + e.getMessage());
99116
}
100117
}
101118

102119
public static void main(String[] args) {
103120
// testGetGroupInfo();
104-
// testGetGroupListByAppkey();
105-
// testUpdateGroupInfo();
106-
// testDeleteGroup();
107-
108121
}
109122
}

example/main/java/cn/jpush/api/examples/IMUserExample.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package cn.jpush.api.jmessage;
1+
package cn.jpush.api.examples;
22

3+
import cn.jpush.api.jmessage.JMessageClient;
34

45
import cn.jpush.api.common.resp.APIConnectionException;
56
import cn.jpush.api.jmessage.base.connection.APIRequestException;
@@ -42,9 +43,11 @@ public static void testRegisterUsers() {
4243
String res = client.registerUsers(users.toArray(regUsers));
4344
System.out.println(res);
4445
} catch (APIConnectionException e) {
45-
e.printStackTrace();
46+
LOG.error("Connection error. Should retry later. ", e);
4647
} catch (APIRequestException e) {
47-
System.out.println(e.getErrorMessage());
48+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
49+
LOG.info("HTTP Status: " + e.getStatus());
50+
LOG.info("Error Message: " + e.getMessage());
4851
}
4952
}
5053

@@ -55,9 +58,11 @@ public static void testGetUserInfo() {
5558
String res = client.getUserInfo("test_user");
5659
System.out.println(res);
5760
} catch (APIConnectionException e) {
58-
e.printStackTrace();
61+
LOG.error("Connection error. Should retry later. ", e);
5962
} catch (APIRequestException e) {
60-
e.printStackTrace();
63+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
64+
LOG.info("HTTP Status: " + e.getStatus());
65+
LOG.info("Error Message: " + e.getMessage());
6166
}
6267
}
6368

@@ -67,9 +72,11 @@ public static void testUpdatePassword() {
6772
try {
6873
client.updateUserPassword("test_user", "test_new_pass");
6974
} catch (APIConnectionException e) {
70-
e.printStackTrace();
75+
LOG.error("Connection error. Should retry later. ", e);
7176
} catch (APIRequestException e) {
72-
e.printStackTrace();
77+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
78+
LOG.info("HTTP Status: " + e.getStatus());
79+
LOG.info("Error Message: " + e.getMessage());
7380
}
7481
}
7582

@@ -79,9 +86,11 @@ public static void testUpdateUserInfo() {
7986
try {
8087
client.updateUserInfo("test_user", "test_nick", "2000-01-12", "help me!", 1, "shenzhen", "nanshan", null);
8188
} catch (APIConnectionException e) {
82-
e.printStackTrace();
89+
LOG.error("Connection error. Should retry later. ", e);
8390
} catch (APIRequestException e) {
84-
e.printStackTrace();
91+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
92+
LOG.info("HTTP Status: " + e.getStatus());
93+
LOG.info("Error Message: " + e.getMessage());
8594
}
8695
}
8796

@@ -92,9 +101,11 @@ public static void testGetUsers() {
92101
String res = client.getUserList(0, 30);
93102
System.out.println(res);
94103
} catch (APIConnectionException e) {
95-
e.printStackTrace();
104+
LOG.error("Connection error. Should retry later. ", e);
96105
} catch (APIRequestException e) {
97-
e.printStackTrace();
106+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
107+
LOG.info("HTTP Status: " + e.getStatus());
108+
LOG.info("Error Message: " + e.getMessage());
98109
}
99110
}
100111

@@ -105,9 +116,11 @@ public static void testGetGroupsByUser() {
105116
String res = client.getGroupListByUser("test_user");
106117
System.out.println(res);
107118
} catch (APIConnectionException e) {
108-
e.printStackTrace();
119+
LOG.error("Connection error. Should retry later. ", e);
109120
} catch (APIRequestException e) {
110-
e.printStackTrace();
121+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
122+
LOG.info("HTTP Status: " + e.getStatus());
123+
LOG.info("Error Message: " + e.getMessage());
111124
}
112125
}
113126

@@ -117,14 +130,16 @@ public static void testDeleteUser() {
117130
try {
118131
client.deleteUser("test_user_119");
119132
} catch (APIConnectionException e) {
120-
e.printStackTrace();
133+
LOG.error("Connection error. Should retry later. ", e);
121134
} catch (APIRequestException e) {
122-
e.printStackTrace();
135+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
136+
LOG.info("HTTP Status: " + e.getStatus());
137+
LOG.info("Error Message: " + e.getMessage());
123138
}
124139
}
125140

126141
public static void main(String[] args) {
127-
testRegisterUsers();
142+
testGetGroupsByUser();
128143
}
129144

130145
}

0 commit comments

Comments
 (0)