Android 如何将来自api的密钥与来自同一api的同一对象下的另一个密钥进行匹配?

Android 如何将来自api的密钥与来自同一api的同一对象下的另一个密钥进行匹配?,android,arrays,json,api,Android,Arrays,Json,Api,我正在构建一个android应用程序测试,我正在学习如何从API获取数据并显示它。 到目前为止,我做得很好。但我似乎不能再往前走了。 我使用的免费api是 https://www.opentdb.com/api.php?amount=10&category=10 我的问题是,我似乎无法将问题与对象的正确答案相匹配。我总是在点击按钮时得到随机的问题和随机的正确答案 这是我的密码 //url = https://www.opentdb.com/api.php?amount=25

我正在构建一个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数据转换为模型类(例如AttemptItem)对象,然后通过该对象访问该对象的问答字段

检查这个答案

您可以通过在此处粘贴JSON来获取该类