Skip to content

Commit 0757918

Browse files
committed
Let it throw Exceptions
1 parent 58c5d10 commit 0757918

File tree

1 file changed

+51
-58
lines changed

1 file changed

+51
-58
lines changed

src/main/java/top/mryan2005/simplifiedjava/flarum/CreateDiscussion.java

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package top.mryan2005.simplifiedjava.flarum;
22

3-
import java.io.BufferedReader;
4-
import java.io.InputStream;
5-
import java.io.InputStreamReader;
6-
import java.io.OutputStream;
3+
import java.io.*;
74
import java.net.HttpURLConnection;
5+
import java.net.MalformedURLException;
6+
import java.net.ProtocolException;
87
import java.net.URL;
98

109
public class CreateDiscussion {
@@ -23,70 +22,64 @@ public CreateDiscussion(String hostUrl, String token, String title, String conte
2322
this.hostUrl = hostUrl;
2423
}
2524

26-
public StringBuffer submit() {
27-
try {
28-
// URL of the server endpoint
29-
URL url = new URL(hostUrl + "/api/discussions");
30-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
25+
public StringBuffer submit() throws IOException {
26+
// URL of the server endpoint
27+
URL url = new URL(hostUrl + "/api/discussions");
28+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
3129

32-
// Set the request method to POST
33-
connection.setRequestMethod("POST");
34-
connection.setDoOutput(true);
30+
// Set the request method to POST
31+
connection.setRequestMethod("POST");
32+
connection.setDoOutput(true);
3533

36-
// Set request headers
37-
connection.setRequestProperty("Content-Type", "application/json");
38-
connection.setRequestProperty("Accept", "application/json");
39-
connection.setRequestProperty("Authorization", "Token " + this.token);
34+
// Set request headers
35+
connection.setRequestProperty("Content-Type", "application/json");
36+
connection.setRequestProperty("Accept", "application/json");
37+
connection.setRequestProperty("Authorization", "Token " + this.token);
4038

41-
// JSON payload
42-
String jsonInputString = String.format(
43-
"{" +
44-
"\"data\": {" +
45-
"\"type\": \"discussions\"," +
46-
"\"attributes\": {" +
47-
"\"title\": \"%s\"," +
48-
"\"content\": \"%s\"" +
49-
"}," +
50-
"\"relationships\": {" +
51-
"\"tags\": {" +
52-
"\"data\": [" +
53-
"%s" +
54-
"]" +
55-
"}" +
39+
// JSON payload
40+
String jsonInputString = String.format(
41+
"{" +
42+
"\"data\": {" +
43+
"\"type\": \"discussions\"," +
44+
"\"attributes\": {" +
45+
"\"title\": \"%s\"," +
46+
"\"content\": \"%s\"" +
47+
"}," +
48+
"\"relationships\": {" +
49+
"\"tags\": {" +
50+
"\"data\": [" +
51+
"%s" +
52+
"]" +
5653
"}" +
5754
"}" +
58-
"}",
59-
title, content, String.join(",", tags)
60-
);
61-
62-
// Send the request
63-
try (OutputStream os = connection.getOutputStream()) {
64-
byte[] input = jsonInputString.getBytes("utf-8");
65-
os.write(input, 0, input.length);
66-
}
55+
"}" +
56+
"}",
57+
title, content, String.join(",", tags)
58+
);
6759

68-
// Get the response code
69-
int responseCode = connection.getResponseCode();
70-
System.out.println("Response Code: " + responseCode);
60+
// Send the request
61+
try (OutputStream os = connection.getOutputStream()) {
62+
byte[] input = jsonInputString.getBytes("utf-8");
63+
os.write(input, 0, input.length);
64+
}
7165

72-
// Handle the response (optional)
73-
InputStream inputStream = connection.getInputStream();
74-
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
75-
String line;
76-
StringBuffer response = new StringBuffer();
77-
while ((line = reader.readLine()) != null) {
78-
response.append(line);
79-
}
80-
reader.close();
81-
return response;
66+
// Get the response code
67+
int responseCode = connection.getResponseCode();
68+
System.out.println("Response Code: " + responseCode);
8269

83-
} catch (Exception e) {
84-
e.printStackTrace();
85-
}
86-
return null;
70+
// Handle the response (optional)
71+
InputStream inputStream = connection.getInputStream();
72+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
73+
String line;
74+
StringBuffer response = new StringBuffer();
75+
while ((line = reader.readLine()) != null) {
76+
response.append(line);
77+
}
78+
reader.close();
79+
return response;
8780
}
8881

89-
public static void main(String[] args) {
82+
public static void main(String[] args) throws IOException {
9083
String hostUrl = "https://xxx.com";
9184
String token = "xxx";
9285
String title = "Hello World";

0 commit comments

Comments
 (0)