A Simple Wrapper for the OpenTDB API in Java Maven. Full Documentation
<dependency>
<groupId>com.github.shinixsensei-dev</groupId>
<artifactId>OpenTDB4j</artifactId>
<version>VERSION</version>
</dependency>
implementation("com.github.shinixsensei-dev:OpenTDB4j:VERSION")
implementation 'com.github.shinixsensei-dev:OpenTDB4j:VERSION'
It's recommended to use the newest Version in the Release Tab.
public static void main(String[] Args) throws LoginException {
OpenTDB obj = new OpenTDB();
obj.setCategory(29);
obj.setDifficulty("easy");
obj.getTrivia();
System.out.println(obj.getQuestion());
System.out.println(obj.getCorrectAnswer());
String[] incorrectAnswers = obj.getIncorrectAnswers();
for (int i = 0; i < obj.incorrectAnswers.length ; i++) {
System.out.println(obj.incorrectAnswers[i]);
}
System.out.println(obj.getCategory());
System.out.println(obj.getDifficulty());
System.out.println(obj.getType());
}
View OpenTDB to see available selections for .setCategory()
and .setDifficulty()
.
First, create a new OpenTDB Object
OpenTDB obj = new OpenTDB();
After that, you can set a category and a difficulty (optional (Will be random if none is set))
obj.setCategory(29);
obj.setDifficulty("easy");
Now run the method by using
obj.getTrivia();
You can now read out the API Response by using obj
as follows
System.out.println(obj.getQuestion());
System.out.println(obj.getCorrectAnswer());
String[] incorrectAnswers = obj.getIncorrectAnswers();
for (int i = 0; i < obj.incorrectAnswers.length ; i++) {
System.out.println(obj.incorrectAnswers[i]);
}
System.out.println(obj.getCategory());
System.out.println(obj.getDifficulty());