-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
66 lines (55 loc) · 2.48 KB
/
Main.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Main extends JFrame {
private JLabel repoNameLabel;
private JTextArea descriptionTextArea;
public Main() {
setTitle("GitHub Repository Description");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
repoNameLabel = new JLabel("Repository: Solving-Challenges-JAVA-SE");
descriptionTextArea = new JTextArea();
descriptionTextArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(descriptionTextArea);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(repoNameLabel, BorderLayout.NORTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
fetchRepoDescription();
setVisible(true);
}
private void fetchRepoDescription() {
try {
URL url = new URL("https://api.github.com/repos/mahendramahara/Solving-Challenges-JAVA-SE");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/vnd.github.v3+json");
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Parse the JSON response to get repository description
String json = response.toString();
String description = json.split("\"description\":")[1].split(",")[0];
description = description.substring(1, description.length() - 1); // Remove surrounding quotes
descriptionTextArea.setText(description);
} else {
descriptionTextArea.setText("Failed to fetch description. Response code: " + responseCode);
}
} catch (IOException e) {
descriptionTextArea.setText("Failed to fetch description. Error: " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}