1
1
package top .mryan2005 .simplifiedjava .flarum ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .InputStream ;
5
- import java .io .InputStreamReader ;
6
- import java .io .OutputStream ;
3
+ import java .io .*;
7
4
import java .net .HttpURLConnection ;
5
+ import java .net .MalformedURLException ;
6
+ import java .net .ProtocolException ;
8
7
import java .net .URL ;
9
8
10
9
public class CreateDiscussion {
@@ -23,70 +22,64 @@ public CreateDiscussion(String hostUrl, String token, String title, String conte
23
22
this .hostUrl = hostUrl ;
24
23
}
25
24
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 ();
31
29
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 );
35
33
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 );
40
38
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
+ "]" +
56
53
"}" +
57
54
"}" +
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
+ );
67
59
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
+ }
71
65
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 );
82
69
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 ;
87
80
}
88
81
89
- public static void main (String [] args ) {
82
+ public static void main (String [] args ) throws IOException {
90
83
String hostUrl = "https://xxx.com" ;
91
84
String token = "xxx" ;
92
85
String title = "Hello World" ;
0 commit comments