|
| 1 | +package top.mryan2005.simplifiedjava.flarum; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | +import java.net.URL; |
| 8 | + |
| 9 | +public class ListDiscussions { |
| 10 | + public String hostUrl; |
| 11 | + |
| 12 | + public ListDiscussions(String hostUrl) { |
| 13 | + this.hostUrl = hostUrl; |
| 14 | + } |
| 15 | + |
| 16 | + public StringBuffer getIt() throws IOException { |
| 17 | + // URL of the server endpoint |
| 18 | + URL url = new URL(this.hostUrl); |
| 19 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 20 | + |
| 21 | + // Set the request method to GET |
| 22 | + connection.setRequestMethod("GET"); |
| 23 | + |
| 24 | + // Set request headers |
| 25 | + connection.setRequestProperty("Accept", "application/json"); |
| 26 | + |
| 27 | + // Get the response code |
| 28 | + int responseCode = connection.getResponseCode(); |
| 29 | + System.out.println("Response Code: " + responseCode); |
| 30 | + |
| 31 | + // Read the response |
| 32 | + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| 33 | + String inputLine; |
| 34 | + StringBuffer response = new StringBuffer(); |
| 35 | + while ((inputLine = in.readLine()) != null) { |
| 36 | + response.append(inputLine); |
| 37 | + } |
| 38 | + in.close(); |
| 39 | + |
| 40 | + return response; |
| 41 | + } |
| 42 | + |
| 43 | + public static void main(String[] args) throws IOException { |
| 44 | + ListDiscussions listDiscussions = new ListDiscussions("https://xxxxx.com/api/discussions"); |
| 45 | + System.out.println(listDiscussions.getIt()); |
| 46 | + } |
| 47 | +} |
0 commit comments