java - Android Volley 循环响应

标签 java android loops android-volley

我正在学习如何使用 Volley。我已将响应成功记录到 System.out.printIn 并在 logcat 中看到它用于数组中的单个项目。当我有多个项目时,我会为 FOR 循环而苦苦挣扎。我无法弄清楚如何遍历数据。有人可以帮我演示如何操作吗。

我的主要 Activity

    package hfad.com.volley_listview_array;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;
public class MainActivity extends Activity {
    ArrayAdapter<String> adapter;
    ArrayList<String> items;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final TextView mTextView = (TextView) findViewById(R.id.textView);

        String url = "https://reqres.in/api/users?page=2";

        JsonObjectRequest jsonRequest = new JsonObjectRequest
                (Request.Method.GET, url, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // the response is already constructed as a JSONObject!
                        try {
                            response = response.getJSONObject("data");

                            //for loop goes here? How? I get i already defined in scope
                            for (int i = 0, i < response.length(), i++);
                            //String site = response.getString("first_name"),
                              //      network = response.getString("last_name");
                            //System.out.println("firstname: "+site+"\nLastname: "+network);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });

        Volley.newRequestQueue(this).add(jsonRequest);


    }}

而我的 activity_main.xml 不需要在列表中显示结果。只想把所有的数据一个接一个地堆起来。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
     <!--
    <ListView
        android:id="@+id/listv"
        android:layout_width="match_parent"
        android:layout_height="89dp"></ListView>
     -->

</LinearLayout>

我的数据来自这个测试 api https://reqres.in/api/users?page=2

{"page":"2","per_page":3,"total":12,"total_pages":4,"data":[{"id":4,"first_name":"eve","last_name":"holt","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"},{"id":5,"first_name":"gob","last_name":"bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"},{"id":6,"first_name":"tracey","last_name":"bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"}]}

我得到了要在我的 logcat 中显示的数据,但它只显示第一项。如何遍历所有记录。感谢您的帮助。

最佳答案

 String url = "https://reqres.in/api/users?page=2";

        JsonObjectRequest jsonRequest = new JsonObjectRequest
                (Request.Method.GET, url, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("data");
                            JSONObject jsonInnerObject;

                            for (int k = 0; k< jsonArray.length(); k++){

                                jsonInnerObject=new JSONObject(jsonArray.get(k).toString());

                                String site =  jsonInnerObject.getString("first_name"),
                                    network =  jsonInnerObject.getString("last_name");
                                System.out.println("firstname: "+site+"\nLastname: "+network);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });

        Volley.newRequestQueue(this).add(jsonRequest);`

注意:data包含JSONObject的JSONArray

关于java - Android Volley 循环响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43576603/

相关文章:

java - 为什么允许抽象类 "DocumentBuilderFactory"实例化新实例

java - NotOLE2FileException : Invalid header signature; read 0x0000000000000000, 预期为 0xE11AB1A1E011CFD0 - 文件似乎不是有效的 OLE2 文档

Android:WAITING super.onPause 直到按下按钮

android - Google Play 和 OpenSSL 警告消息

javascript - React——为什么循环状态更新先于递归状态更新?

java - Recaptcha 验证 : IOException : Tried all: 1 addresses, 但无法通过 HTTPS 连接到服务器 : google. com 端口:443

android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java :3147)

c# - 运行两个 foreach 循环

php - 来自另一个函数的数组中的引用检测

java - 什么是<? super T> 语法?