import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; import java.io.StringReader; public class WeatherApp extends JFrame { private JLabel cityLabel; private JTextField cityField; private JButton getWeatherButton; private JTextArea weatherTextArea; private static final String API_KEY = "WMxvv0mQNHNSC1j5bW9BDu1PJVbzX9s"; private static final String API_URL = "https://api.tomorrow.io/v4/weather/forecast?location=42.3478,-71.0466&apikey=WMxvv0mQNNSC1j5bW9BDu1fPJVbzX9s"; public WeatherApp() { setTitle("Weather App"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initComponents(); addListeners(); setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); add(cityLabel); add(cityField); add(getWeatherButton); add(new JScrollPane(weatherTextArea)); setLocationRelativeTo(null); setVisible(true); } private void initComponents() { cityLabel = new JLabel("Enter City:"); cityField = new JTextField(); getWeatherButton = new JButton("Get Weather"); weatherTextArea = new JTextArea(); weatherTextArea.setEditable(false); } private void addListeners() { getWeatherButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String cityName = cityField.getText(); String weatherInfo = getWeatherInfo(cityName); weatherTextArea.setText(weatherInfo); } }); } private String getWeatherInfo(String cityName) { try { String apiUrl = String.format(API_URL, cityName, API_KEY); URL url = new URL(apiUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); System.out.println(response); } reader.close(); return parseWeatherData(response.toString()); } else { return "Error: Unable to fetch weather data."; } } catch (Exception ex) { ex.printStackTrace(); return "Error: " + ex.getMessage(); } } private String parseWeatherData(String jsonData) { try { // Parse JSON using javax.json JsonReader jsonReader = Json.createReader(new StringReader(jsonData)); JsonObject json = jsonReader.readObject(); // Extract relevant information from the JSON object JsonObject weatherData = json.getJsonObject("data"); JsonObject currentConditions = weatherData.getJsonObject("current"); String temperature = currentConditions.getString("temperature"); // Build a formatted message with the extracted information return "Current Temperature: " + temperature + "°C"; } catch (Exception e) { e.printStackTrace(); return "Error parsing weather data."; } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new WeatherApp(); } }); } }