JOpenTDB is Lightweight, Fast , Easy To Use Sync and Async Wrapper for https://opentdb.com (an open source trivia api)
Easy to add in your projects using gradle, maven or jar
- Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Add the dependency (replace version with the version on top)
dependencies {
implementation 'com.github.shivam1608:JOpenTDB:0.1.0'
}
- Add the repository in pom.xml file
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
- Add the dependency (replace version with the version on top)
<dependency>
<groupId>com.github.shivam1608</groupId>
<artifactId>JOpenTDB</artifactId>
<version>0.1.0</version>
</dependency>
Download the jar from this repo
out/artifact/JOpenTDB.jar or Use the Release Section
- Get 10 Questions for Trivia (Synchronous)
import me.shivzee.OpenTDB;
import me.shivzee.util.Question;
import me.shivzee.exceptions.TokenNotFoundException;
class TriviaExample {
public static void main(String [] args){
try{
OpenTDB openTDB = new OpenTDB();
for (Question question : openTDB.getTrivia()){
System.out.println("Question "+question.getQuestion());
System.out.println("Answer "+question.getAnswer());
}
}catch (TokenNotFoundException exception){
// Can Ignore Until using Session Tokens
}
}
}
- Get 10 Questions for Trivia (Asynchronous)
import me.shivzee.OpenTDB;
import me.shivzee.util.Question;
class TriviaExample {
public static void main(String [] args){
OpenTDB openTDB = new OpenTDB();
openTDB.getTriviaAsync((questions)->{
for (Question question : questions){
System.out.println("Question "+question.getQuestion());
System.out.println("Answer "+question.getAnswer());
}
});
}
}
- Without Session Token
import me.shivzee.OpenTDB;
OpenTDB trivia = new OpenTDB();
- With Session Token
import me.shivzee.OpenTDB;
import me.shivzee.session.Session;
OpenTDB trivia = new OpenTDB(Session.create());
- the Question class
String triviaQuestion = question.getQuestion(); // Get The Question
String answer = question.getAnswer(); // Get The Answer To That Question
List<String> options = question.getOptions(); // Other Incorrect Answers
Type type = question.getType(); // Type of the Question Multiple/Boolean
Difficulty difficulty = question.getDifficulty(); // Difficulty
Category category = question.getCategory(); // The Category of The Question
- Just Specify the Limit
List<Question> questions = trivia.getTrivia(10 );
trivia.getTriviaAsync(10 , (triviaQuestions)->{});
- You can specify Attributes to The Questions
Category category = Category.RANDOM;
Difficulty difficulty = Difficulty.HARD;
Encoding encoding = Encoding.DEFAULT;
Type type = Type.MULTIPLE;
List<Question> questions = trivia.getTrivia(10 , category , type , encoding ,difficulty);
trivia.getTriviaAsync(10 , category , type, encoding , difficulty , (triviaQuestions)->{});
Type.RANDOM; // Mixed Type of Questions
Type.MULTIPLE; // Quesion with 3 Wrong Options and 1 Answer
Type.BOOLEAN; // True False Questions
Difficulty.RANDOM; // Random Difficulty Questions
Difficulty.HARD; // Hard Questions
Difficulty.MEDIUM; // Medium Questions
Difficulty.EASY; // Easy Questions
- use Default if not Experienced
Encoding.DEFAULT; // Recomended for new ones
Encoding.URL_ENCODED; // IDK Why Someone need it
Encoding.BASE64; // Best as can be decoded easily
- using base64 is recommended which can be easily decoded
- Refer to JavaDocs for All Category
Category.GENERAL_KNOWLEDGE;
Category.SCIENCE_COMPUTERS;
Category.SCIENCE_GADGETS;
Category.ENTERTAINMENT_VIDEO_GAMES;
// More Category Check Docs
int totalCount = Category.SCIENCE_COMPUTERS.getTotalCount();
- Create a Session Token
import me.shivzee.OpenTDB;
import me.shivzee.session.Session;
OpenTDB trivia = new OpenTDB(Session.create());
- OR Set it
import me.shivzee.OpenTDB;
import me.shivzee.session.Session;
OpenTDB trivia = new OpenTDB();
trivia.setSessionToken(Session.create());
- Get Current Token Using
import me.shivzee.OpenTDB;
import me.shivzee.session.Session;
OpenTDB trivia = new OpenTDB();
trivia.setSessionToken(Session.create());
trivia.getSessionToken();
- Reset / Destroy a Token
import me.shivzee.OpenTDB;
import me.shivzee.session.Session;
Session.destroy("TOKEN HERE");
- Know more about session tokens on API Docs of OpenTDB API
Author : Shizee IDE Used : IntelliJ