Skip to content

Instantly share code, notes, and snippets.

@nezarjhons
Forked from Da9el00/APIConnector.java
Created October 31, 2021 02:25
Show Gist options
  • Save nezarjhons/0594ee294da6619d0ad3307dd80dafee to your computer and use it in GitHub Desktop.
Save nezarjhons/0594ee294da6619d0ad3307dd80dafee to your computer and use it in GitHub Desktop.
JavaFX weather app setuop using maven
package org.example;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class APIConnector {
private final String urlString;
public APIConnector(String urlString) throws MalformedURLException {
this.urlString = urlString;
}
public JSONArray getJSONArray(String query){
try {
URL url = new URL(urlString + query);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
//Check if connect is made
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("HttpResponseCode: " + responseCode);
} else {
StringBuilder informationString = new StringBuilder();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
informationString.append(scanner.nextLine());
}
scanner.close();
JSONParser parse = new JSONParser();
return (JSONArray) parse.parse(String.valueOf(informationString));
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
public JSONObject getJSONObject(String query){
try {
URL url = new URL(urlString + query);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
//Check if connect is made
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("HttpResponseCode: " + responseCode);
} else {
StringBuilder informationString = new StringBuilder();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
informationString.append(scanner.nextLine());
}
scanner.close();
JSONParser parse = new JSONParser();
return (JSONObject) parse.parse(String.valueOf(informationString));
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package org.example;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
/**
* JavaFX App
*/
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
scene = new Scene(loadFXML("primary"));
stage.setScene(scene);
stage.show();
}
static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>WeatherAPP</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>15.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>15.0.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>org.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.PrimaryController">
<children>
<TextField fx:id="cityInput" layoutX="194.0" layoutY="133.0" />
<Button layoutX="354.0" layoutY="133.0" mnemonicParsing="false" onAction="#getWeatherData" text="Button" />
<Text fx:id="weatherText" layoutX="52.0" layoutY="289.0" strokeType="OUTSIDE" strokeWidth="0.0">
<font>
<Font size="25.0" />
</font>
</Text>
<Text layoutX="171.0" layoutY="71.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Today's Weather">
<font>
<Font size="35.0" />
</font>
</Text>
</children>
</AnchorPane>
package org.example;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.net.MalformedURLException;
public class PrimaryController {
@FXML
private TextField cityInput;
@FXML
private Text weatherText;
private final String cityAPI = "https://www.metaweather.com/api/location/search/?query=";
private final String weatherAPI = "https://www.metaweather.com/api/location/";
@FXML
void getWeatherData(ActionEvent event) throws MalformedURLException {
JSONObject todaysWeather = GetTodaysWeatherInformation(getWoeid());
System.out.println(todaysWeather);
weatherText.setText(
"Min temperature: " + todaysWeather.get("min_temp") +
"\nCurrent temperature: " + todaysWeather.get("the_temp") +
"\nMax temperature: " + todaysWeather.get("max_temp")
);
}
public String getWoeid() throws MalformedURLException {
APIConnector apiConnectorCity = new APIConnector(cityAPI);
JSONObject jsonData = (JSONObject) (apiConnectorCity.getJSONArray(cityInput.getText())).get(0);
return jsonData.get("woeid").toString();
}
public JSONObject GetTodaysWeatherInformation(String woeid) throws MalformedURLException {
APIConnector apiConnectorWeather = new APIConnector(weatherAPI);
JSONObject weatherJSONObject = apiConnectorWeather.getJSONObject(woeid + "/");
JSONArray weatherArray = (JSONArray) weatherJSONObject.get("consolidated_weather");
return (JSONObject) weatherArray.get(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment