I am creating a Quiz app on android studio and the questions will be called from an URL containing a JSONObject, and since these API calls happen asynchronously, i have to make sure that my app is waiting for the server to respond with a callback, below is the parsing method i created following some internet tutorials, it would be nice if you could help me understand which changes should i apply
private void jsonParse(){
final Question[] quest =new Question[10];
String url="https://opentdb.com/api.php?amount=10";
JsonObjectRequest request =new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray ja=response.getJSONArray("results");
for(int i=0;i<3;i++){
JSONObject temp_quest=ja.getJSONObject(i);
String question =temp_quest.getString("question");
String correctanswer=temp_quest.getString("correct_answer");
String incorrectanswer_1=(String)temp_quest.getJSONArray("incorrect_answers").getString(0);
String incorrectanswer_2=(String)temp_quest.getJSONArray("incorrect_answers").getString(1);
String incorrectanswer_3=(String)temp_quest.getJSONArray("incorrect_answers").getString(2);
String[] temp=new String[3];
temp[0]=incorrectanswer_1;
temp[1]=incorrectanswer_2;
temp[2]=incorrectanswer_3;
quest[i]=new Question(question,correctanswer,temp);
mTextViewResult.append(quest[i].getQuestion()+" \n"+ quest[i].getCorrectAnswer()+"\n "+
quest[i].getAnswer(1)+"\n "+quest[i].getAnswer(2)+"\n "+
quest[i].getAnswer(3)+"\n" +quest[i].getAnswer(4)+"\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
}