如何使用volley for android存储restapi中的数据

js4nwp54  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(368)

我尝试从我的api获取数据,我可以获取数据并将其转换为我创建的对象类并显示它,但我不能将所有对象示例存储在一个列表中。有人知道吗?我尝试添加列表中的每个元素,然后尝试从中获取元素,但不起作用,列表保持为空。这是api返回的结果:

{"_id":"60683c5bcdfcb74689bc8382","questionId":3,"question":"Question 3","choices":"1-/-2-/-3-/-4","category":"Other","difficulty":"Easy"}

这是我的“apitest活动”:

package com.jojo.jojozquizz;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.jojo.jojozquizz.model.Question;
import com.jojo.jojozquizz.model.QuestionBank;
import com.jojo.jojozquizz.tools.APIListener;

import org.json.JSONException;
import org.json.JSONObject;

public class ApiTests extends AppCompatActivity implements APIListener {

    private TextView text;

    public static final String TAG = "MyTag";

    private QuestionBank mQuestionBank;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_api_tests);
        mQuestionBank = new QuestionBank();
        text = findViewById(R.id.api_text);

        String url = "https://nextfor.studio/questions/test";

        getQuestionFromApi(url);
    }

    private void getQuestionFromApi(String url) {
        APIListener listener = this;
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    Log.d(TAG, "onResponse: " + response.toString());
                    Question question = new Question();
                    question.setId(response.getInt("questionId"));
                    question.setQuestion(response.getString("question"));
                    question.setChoices(response.getString("choices"));
                    question.setCategory(response.getString("category"));
                    question.setDifficulty(response.getString("difficulty"));

                    listener.questionReceived(question);
                } catch (JSONException e) {
                    Log.i(TAG, e.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.d(TAG, "onErrorResponse: " + error.getMessage());
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    @Override
    public void questionReceived(Question q) {
        mQuestionBank.addQuestion(q);
    }
}

问题类别:

package com.jojo.jojozquizz.model;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

import java.io.Serializable;
import java.util.List;

@Entity(tableName = "questions")
public class Question implements Serializable {

    @PrimaryKey()
    @ColumnInfo(name = "id")
    private long id;

    @ColumnInfo(name = "question")
    private String mQuestion;

    @Ignore
    private List<String> mChoiceList;

    @ColumnInfo(name = "choices")
    private String mChoices;

    @ColumnInfo(name = "answer_index")
    private int mAnswerIndex;

    @ColumnInfo(name = "categorie")
    private String mCategory;

    @Ignore
    private String mTrueAnswer;

    @ColumnInfo(name = "difficulty")
    private String mDifficulty;

    public Question() {
    }

    public Question(int id, String mQuestion, List<String> mChoiceList, String mCategorie, String mDifficulty) {
        this.id = id;
        this.mQuestion = mQuestion;
        this.mChoices = mChoiceList.get(0) + "-/-" + mChoiceList.get(1) + "-/-" + mChoiceList.get(2) + "-/-" + mChoiceList.get(3);
        this.mAnswerIndex = 0;
        this.mCategory = mCategorie;
        this.mDifficulty = mDifficulty;
    }

    public Question(String mQuestion, List<String> mChoiceList, String mCategorie, String mDifficulty) {
        this.mQuestion = mQuestion;
        this.mChoices = mChoiceList.get(0) + "-/-" + mChoiceList.get(1) + "-/-" + mChoiceList.get(2) + "-/-" + mChoiceList.get(3);
        this.mAnswerIndex = 0;
        this.mCategory = mCategorie;
        this.mDifficulty = mDifficulty;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getQuestion() {
        return mQuestion;
    }

    public void setQuestion(String question) {
        mQuestion = question;
    }

    public List<String> getChoiceList() {
        return mChoiceList;
    }

    public void setChoiceList(List<String> choiceList) {
        mChoiceList = choiceList;
    }

    public String getChoices() {
        return mChoices;
    }

    public void setChoices(String choices) {
        mChoices = choices;
    }

    public void setAnswerIndex(int mAnswerIndex) {
        this.mAnswerIndex = mAnswerIndex;
    }

    public int getAnswerIndex() {
        return mAnswerIndex;
    }

    public String getCategory() {
        return mCategory;
    }

    public void setCategory(String categorie) {
        mCategory = categorie;
    }

    public String getTrueAnswer() {
        return mTrueAnswer;
    }

    public void setTrueAnswer(String trueAnswer) {
        mTrueAnswer = trueAnswer;
    }

    public String getDifficulty() {
        return mDifficulty;
    }

    public void setDifficulty(String difficulty) {
        mDifficulty = difficulty;
    }
}

问题库(类似于问题列表):

package com.jojo.jojozquizz.model;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class QuestionBank {
  private List<Question> mQuestionList;
  public int mNextQuestionIndex;

  public QuestionBank() {
    mQuestionList = new LinkedList<>();
    mNextQuestionIndex = 0;
  }

  public QuestionBank(List<Question> questionList, boolean shuffle) {
    mQuestionList = questionList;
    if (shuffle)
      Collections.shuffle(mQuestionList);
    mNextQuestionIndex = 0;
  }

  public Question getQuestion() {
    if (mNextQuestionIndex == mQuestionList.size())
      mNextQuestionIndex = 0;

    return mQuestionList.get(mNextQuestionIndex++);
  }

  public Question getQuestion(int i) {
    return  mQuestionList.get(i);
  }

  public void reShuffle() {
    Collections.shuffle(mQuestionList);
  }

  public void addQuestion(Question question) {
    mQuestionList.add(question);
  }

  public int returnListSize() {
    return mQuestionList.size();
  }
}

和听众:

package com.jojo.jojozquizz.tools;

import com.jojo.jojozquizz.model.Question;

public interface APIListener {
    void questionReceived(Question q);
}
tzxcd3kk

tzxcd3kk1#

api调用是一个异步过程。所以呢 getQuestionFromApi(url); 可能需要一些时间,所以如果你尝试 text.setText(mQuestionBank.getQuestion(0).getQuestion()); 之后 getQuestionFromApi(url); 行,不行。你可以用 LiveData 来处理这个案子。比如:

class QuestionBank {
    MutableLiveData<List<Question>> mQuestionList;
    public int mNextQuestionIndex;

    public QuestionBank() {
        mQuestionList = new MutableLiveData<>(new LinkedList<>());
        mNextQuestionIndex = 0;
    }

    public QuestionBank(List<Question> questionList, boolean shuffle) {
        mQuestionList.setValue(questionList);
        if (shuffle)
            Collections.shuffle(mQuestionList.getValue());
        mNextQuestionIndex = 0;
    }

    public Question getQuestion() {
        if (mNextQuestionIndex == mQuestionList.getValue().size())
            mNextQuestionIndex = 0;

        return mQuestionList.getValue().get(mNextQuestionIndex++);
    }

    public Question getQuestion(int i) {
        return  mQuestionList.getValue().get(i);
    }

    public void reShuffle() {
        Collections.shuffle(mQuestionList.getValue());
    }

    public void addQuestion(Question question) {
        List<Question> tempQ =  mQuestionList.getValue();
        tempQ.add(question);
        mQuestionList.setValue(tempQ);
    }

    public int returnListSize() {
        return mQuestionList.getValue().size();
    }
}

还有里面 onCreate() 观察是否添加了任何数据,然后 setText()TextView . 比如:

QuestionBank questionBank = new QuestionBank();
     questionBank.mQuestionList.observe(getViewLifecycleOwner(), new Observer<List<Question>>() {
          @Override
          public void onChanged(List<Question> questions) {
          if (!questions.isEmpty()){
               textView.setText(questions.get(0).getQuestion());
                Log.d("TAG==>>","New Question added  Total size = "+questions.size());
                }
            }
      });

相关问题