I am building an android app quiz, i am learning how to get data from API and display it. So far, i am doing good. But i can't seems to get further. The free api i am using is this
https://www.opentdb.com/api.php?amount=10&category=10 My problem is i can't seems to match the question and the correct answer from the object. I always get random question and random correct answer on button click
This is my code
//url = https://www.opentdb.com/api.php?amount=25 public class MainActivity extends AppCompatActivity { private TextView textData; private TextView answer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btnId); textData = (TextView) findViewById(R.id.txtV); answer = (TextView) findViewById(R.id.answerCorrect); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //executing the AsyncTask on button click new JSONTask().execute("https://www.opentdb.com/api.php?amount=10&category=10&difficulty=easy&type=multiple"); } }); } class JSONTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { HttpURLConnection connection = null; StringBuffer stringBuffer; BufferedReader bufferedReader = null; try { //passing url here URL url = new URL(params[0]); //making a connection connection = (HttpURLConnection) url.openConnection(); //connecting to the server connection.connect(); //getting object from server and storing to inputStream InputStream stream = connection.getInputStream(); //buffer reader to read the data from inputstream bufferedReader = new BufferedReader(new InputStreamReader(stream)); //holding data stringBuffer = new StringBuffer(); String line = ""; while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line); } //toString will be passed to the onPostExecute result value //return completed JSON object String finalOBJ = stringBuffer.toString(); //passing entire json object in new JSON object instance. JSONObject jsonObject = new JSONObject(finalOBJ); //getting the array from the main object JSONArray jsonArray = jsonObject.getJSONArray("results"); //passing the desired index from the results JSONObject fobj = jsonArray.getJSONObject(3); JSONObject ansobj = jsonArray.getJSONObject(4); //getting the key from results array String question = fobj.getString("question"); return question; } catch (IOException | JSONException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } @Override //here result will take the data protected void onPostExecute(String result) { super.onPostExecute(result); textData.setText(result); } } JSON APi i am taking from that site.
What i want to achieve is the show the question with the correct answer from same object.
{ "response_code":0, "results":[ { "category":"Entertainment: Books", "type":"multiple", "difficulty":"easy", "question":"George Orwell wrote this book, which is often considered a statement on government oversight.", "correct_answer":"1984", "incorrect_answers":[ "The Old Man and the Sea", "Catcher and the Rye", "To Kill a Mockingbird" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"hard", "question":"Which author and poet famously wrote the line, "The female of the species is more deadly than the male"?", "correct_answer":"Rudyard Kipling", "incorrect_answers":[ "Edgar Allan Poe", "William Shakespeare", "William Wordsworth" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"hard", "question":"In the book "The Martian", how long was Mark Watney trapped on Mars (in Sols)?", "correct_answer":"549 Days", "incorrect_answers":[ "765 Days", "401 Days", "324 Days" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"hard", "question":"In the Harry Potter universe, what is Cornelius Fudge's middle name?", "correct_answer":"Oswald", "incorrect_answers":[ "James", "Harold", "Christopher" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"hard", "question":"In the Harry Potter universe, who does Draco Malfoy end up marrying?", "correct_answer":"Astoria Greengrass", "incorrect_answers":[ "Pansy Parkinson", "Millicent Bulstrode", "Hermione Granger" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"hard", "question":"What is Hermione Granger's middle name?", "correct_answer":"Jean", "incorrect_answers":[ "Jane", "Emma", "Jo" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"hard", "question":"What is Ron Weasley's middle name?", "correct_answer":"Bilius", "incorrect_answers":[ "Arthur", "John", "Dominic" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"medium", "question":"What was the name of the Mysterious Island, in Jules Verne's "The Mysterious Island"?", "correct_answer":"Lincoln Island", "incorrect_answers":[ "Vulcania Island", "Prometheus Island", "Neptune Island" ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"easy", "question":"What's Harry Potter's dad's name?", "correct_answer":"James Potter", "incorrect_answers":[ "Joey Potter", "Frank Potter", "Hairy Potter Sr." ] }, { "category":"Entertainment: Books", "type":"multiple", "difficulty":"medium", "question":"The book "The Little Prince" was written by...", "correct_answer":"Antoine de Saint-Exupéry", "incorrect_answers":[ "Miguel de Cervantes Saavedra", "Jane Austen", "F. Scott Fitzgerald" ] } ] } }