尝试解释请求 JSON 对象和请求队列的 java for 循环

发布于 2025-01-12 18:13:28 字数 1739 浏览 1 评论 0原文

我正在尝试对一些代码做一份书面报告,我在 Youtube 上找到了一个。但是,我不明白其中一些循环是如何工作的。据我所知,它会遍历列表中的每个项目并获取每个变量的每个值,然后将所有值添加到一个列表中,该列表显示在 Android studio 的 XML 视图中。如果有人能分解正在发生的事情,我们将不胜感激!

private void setupData() {
        RequestQueue queue = Volley.newRequestQueue(this);
        String url =" - hidden - ";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length() ;  i++){
                                JSONObject jo = jsonArray.getJSONObject(i);
                                System.out.println(jo.toString());
                                Supplier supplier = new Supplier(String.valueOf(jo.getInt("id")), jo.getString("name"), jo.getString("url"), jo.getString("specialization"), jo.getString("country"), jo.getInt("rating"));
                                supplierList.add(supplier);
                                System.out.println(jsonArray.length());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println(error.toString());
                System.out.println("That didn't work!");
            }
        });
        queue.add(request);
    }

I'm trying to do a written report on some code and I found one on Youtube. However, I don't understand how some of this loop works. I understand that it goes through every item in the list and fetches each value for each variable and that it then adds all values to a list which is presented in an XML view in Android studio. if someone could breakdown what is happening it would be greatly appreciated!

private void setupData() {
        RequestQueue queue = Volley.newRequestQueue(this);
        String url =" - hidden - ";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length() ;  i++){
                                JSONObject jo = jsonArray.getJSONObject(i);
                                System.out.println(jo.toString());
                                Supplier supplier = new Supplier(String.valueOf(jo.getInt("id")), jo.getString("name"), jo.getString("url"), jo.getString("specialization"), jo.getString("country"), jo.getInt("rating"));
                                supplierList.add(supplier);
                                System.out.println(jsonArray.length());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println(error.toString());
                System.out.println("That didn't work!");
            }
        });
        queue.add(request);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2025-01-19 18:13:28

尽管您可以简单地阅读有关 JSONObject 类和属于该包的所有其他类的信息。但是,让我在这里用一个例子来表达我的理解。
这是正在接收的响应 json。

{
  "data": [
  {
   "id": 1,
   "name": "ABCD",
   "url": "https://www.stackoverflow.com",
   "specialization": "master",
   "country": "India",
   "rating" : 5
  },
  {
   "id": 1,
   "name": "ABCD",
   "url": "https://www.stackoverflow.com",
   "specialization": "master",
   "country": "India",
   "rating" : 5
  }]
}

该代码正在尝试处理这个完整的 json。
它首先将“数据”对象读入数组,因为它代表一个数组,然后将该数组中的每个对象块转换为供应商模型类,然后将其添加到供应商列表中。

JSONArray jsonArray = response.getJSONArray("data"); // reads the "data" attribute.
                        for (int i = 0; i < jsonArray.length() ;  i++){ // Iterates every block, every block inside this array represent a JSONObject
                            JSONObject jo = jsonArray.getJSONObject(i); // Reads every block using simple loop and index logic
                            System.out.println(jo.toString());
                            Supplier supplier = new Supplier(String.valueOf(jo.getInt("id")), jo.getString("name"), jo.getString("url"), jo.getString("specialization"), jo.getString("country"), jo.getInt("rating")); // Reads the attributes from the JSONObject to create an instance of Supplier class
                            supplierList.add(supplier); // Adds the supplier instance to the list
                            System.out.println(jsonArray.length());
                        }

Though you can simply read about the JSONObject class and all other classes belonging to the package. But, let me put it what I understand with an example here.
Here is the response json that is being received.

{
  "data": [
  {
   "id": 1,
   "name": "ABCD",
   "url": "https://www.stackoverflow.com",
   "specialization": "master",
   "country": "India",
   "rating" : 5
  },
  {
   "id": 1,
   "name": "ABCD",
   "url": "https://www.stackoverflow.com",
   "specialization": "master",
   "country": "India",
   "rating" : 5
  }]
}

The code is trying to process this complete json.
It starts with reading the "data" object into an array since it represents an array and then convert every object block in that array to a Supplier model class and then add it into SupplierList.

JSONArray jsonArray = response.getJSONArray("data"); // reads the "data" attribute.
                        for (int i = 0; i < jsonArray.length() ;  i++){ // Iterates every block, every block inside this array represent a JSONObject
                            JSONObject jo = jsonArray.getJSONObject(i); // Reads every block using simple loop and index logic
                            System.out.println(jo.toString());
                            Supplier supplier = new Supplier(String.valueOf(jo.getInt("id")), jo.getString("name"), jo.getString("url"), jo.getString("specialization"), jo.getString("country"), jo.getInt("rating")); // Reads the attributes from the JSONObject to create an instance of Supplier class
                            supplierList.add(supplier); // Adds the supplier instance to the list
                            System.out.println(jsonArray.length());
                        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文