【问题标题】:Android Async interface to get data obtained fron json requestAndroid Async接口获取json请求获取的数据
【发布时间】:2019-10-15 16:02:33
【问题描述】:

我是 Android 新手,这是类问题库返回从 api 接收的 json obj 列表 接口中的ArrayAsyncResponse只包含一个方法进程完成,我读到http请求是异步的但无法关联

问题是模型类 案例1)当不存在ArrayAsyncResponse接口并且我将列表返回到主活动并打印它时,它显示空列表但是当我调用callback.processComplete()然后返回列表然后打印时,它显示数据 case2 ) 如果我在这个函数 callback.processComplete() 中传递 null 则返回的列表再次为空 那么基本上接口在帮助我们的是什么

公开课 QuestionBank { ArrayList questionArrayList = new ArrayList(); 私有字符串 url = "https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements-data.json";

public List<Question> getQuestions(final AnswerListAsyncResponse callBack) {

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
            Request.Method.GET,
            url,
            (JSONArray) null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            Question question = new Question();
                            question.setAnswer(response.getJSONArray(i).get(0).toString());
                            question.setAnswerTrue(response.getJSONArray(i).getBoolean(1));



                            //Add question objects to list
                            questionArrayList.add(question);
                            //Log.d("Hello", "onResponse: " + question.getAnswer());


                           // Log.d("JSON", "onResponse: " + response.getJSONArray(i).get(0));
                            //Log.d("JSON2", "onResponse: " + response.getJSONArray(i).getBoolean(1));

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    if (null != callBack) callBack.processFinished(questionArrayList);

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    AppController.getInstance().addToRequestQueue(jsonArrayRequest);


    return questionArrayList;

}
public class MainActivity extends AppCompatActivity {
private QuestionBank questionBank;
List<Question> questionList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    questionBank=new QuestionBank();
    questionList=questionBank.getQuestions(new AnswerListAsyncResponse() {
        @Override
        public void processFinished(ArrayList<Question> questionsArrayList) {//this function triggers when response is received from api
            Log.d("inside", "processFinished: "+questionsArrayList);
        }
    });
    Log.d("sync response", "questionLIst: "+questionList);

}

}

【问题讨论】:

    标签: android


    【解决方案1】:

    对不起,我不能在评论中写这个,但我想我可以帮助你。

    我不太明白你的问题。但我认为您需要在课堂活动中获得异步监听器。

    你可以用 EventBus 做到这一点,像这样:https://github.com/greenrobot/EventBus

    【讨论】:

    • 是一个不错的选择,但我很想了解内部工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 2014-05-19
    • 2016-03-10
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多