我正在构建一个Android应用测验,我正在学习如何从API获取数据并显示它。 到目前为止,我做得很好。但我似乎无法进一步发展。 我正在使用的免费API是
https://www.opentdb.com/api.php?amount=10&category=10
我的问题是我似乎无法匹配问题和对象的正确答案。我总是在点击按钮
上得到随机问题和随机正确答案这是我的代码
//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我从该网站上获取。
我想要实现的是用同一个对象的正确答案显示问题。
{
"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"
]
}
]
}
&#13;
}
答案 0 :(得分:0)
您可以将JSON数据转换为Model类(例如AttemptItem)对象,然后访问问题&amp;通过对象回答它的领域。
检查此答案How do I convert a JSONObject to class object?
您可以通过粘贴JSON http://www.jsonschema2pojo.org/
来获取课程