Android:为什么我不能在json中循环? [英] Android : why I can't loop in json?

查看:99
本文介绍了Android:为什么我不能在json中循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel代码:

Laravel code:

$Questions = (Questions::all());
$arr = array();
foreach ($Questions as $Question){
  $arr[] = $Question ;
}

return $arr ;







和排球(android)






and volley (android)

public void getMainQuestions(final OnRecievedMainQuestion onRecievedMainQuestion) {

          JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, "http://192.168.1.4:8000/api/mainQuestion", null, new Response.Listener<JSONArray>() {
              @Override
              public void onResponse(JSONArray response) {
                  Question question = new Question();
                  ArrayList<Question> questions = new ArrayList<>();
                    for (int i = 0; i < response.length(); i++) {
                      try {
                          question.setId(response.getJSONObject(i).getInt("id"));
                          question.setTitle(response.getJSONObject(i).getString("title"));
                          question.setContent(response.getJSONObject(i).getString("content"));
                          questions.add(question);
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
                  onRecievedMainQuestion.Recivied(questions);

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

              }
          });
          request.setRetryPolicy(new DefaultRetryPolicy(18000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
          Volley.newRequestQueue(context).add(request);

  }





我的尝试:



我正在尝试使用Android的Volley库从服务器(Laravel)获取数据,但只是在jsonObjects中获取一次数据(我有一个json,我有几个
jsonObject)但我无法从jsonObject获取所有项目。为什么会这样?



What I have tried:

i'm trying to getting data from server(Laravel) using Volley library of android but just getting once of data in jsonObjects(i has one json and i have serveral
jsonObject in JsonArray) but i can't get all of items from jsonObject. why this is happen?

推荐答案

Questions =(Questions :: all());
Questions = (Questions::all());


arr = array();
foreach(
arr = array(); foreach (


问题为


这篇关于Android:为什么我不能在json中循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆