在Android Gridview中显示JSON数据的问题

问题描述 投票:-1回答:1

我无法使用凌空库通过URL在Android应用程序的Gridview中显示JSON数据。我得到:

com.android.volley.NoConnectionError:java.io.IOException

https://reqres.in/api/users?page=%1 $ s中的JSON数据>

{
    "page": 1,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
        "id": 1,
        "email": "[email protected]",
        "first_name": "George",
        "last_name": "Bluth",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
        },
        {
        "id": 2,
        "email": "[email protected]",
        "first_name": "Janet",
        "last_name": "Weaver",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
        },
        {
        "id": 3,
        "email": "[email protected]",
        "first_name": "Emma",
        "last_name": "Wong",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg"
        },
        {
        "id": 4,
        "email": "[email protected]",
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
       },
       {
        "id": 5,
        "email": "[email protected]",
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
       },
       {
        "id": 6,
        "email": "[email protected]",
        "first_name": "Tracey",
        "last_name": "Ramos",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
    }
    ],
    {
     "page": 1,
     "per_page": 6,
     "total": 12,
     "total_pages": 2,
    "data": [
        {
        "id": 1,
        "email": "[email protected]",
        "first_name": "George",
        "last_name": "Bluth",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
        },
        {
        "id": 2,
        "email": "[email protected]",
        "first_name": "Janet",
        "last_name": "Weaver",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
       },
        {
        "id": 3,
        "email": "[email protected]",
        "first_name": "Emma",
        "last_name": "Wong",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg"
    },
    {
        "id": 4,
        "email": "[email protected]",
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
       },
        {
        "id": 5,
        "email": "[email protected]",
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
       },
       {
        "id": 6,
        "email": "[email protected]",
        "first_name": "Tracey",
        "last_name": "Ramos",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
      }
      ],
      "ad": {
    "company": "StatusCode Weekly",
    "url": "http://statuscode.org/",
    "text": "A weekly newsletter focusing on software development, infrastructure, the server, 
   performance, and the stack end of things."
   }
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    Intent intent;
    RequestQueue queue;

    Activity activity;
    Context context;

    GridView gridView;

    private Toolbar mToolbar;

    ProductViewAdapter gridAdapter;

    public ArrayList<Product> products = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);

        activity = this;
        context = getApplicationContext();
        String url = "https://reqres.in/api/users?page=%1$s";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(response);
                        JSONObject results = jsonObject.getJSONObject("results");
                        JSONArray data = results.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            JSONObject jo = data.getJSONObject(i);
                            int id = jo.getInt("id");
                            String first_name = jo.getString("first_name");
                            String last_name = jo.getString("last_name");
                            String avatar = jo.getString("product_image");
                            String email = jo.getString("email");
                            products.add(new Product(id, first_name, last_name, avatar, email));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    GridView gridView = (GridView) findViewById(R.id.gridView);
                    gridAdapter = new ProductViewAdapter(MainActivity.this, R.layout.product_item_layout, products);
                    gridView.setAdapter(gridAdapter);
                    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                        }
                    });
                }
            }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   Log.i("Api Error", "Api Not working " + error);
               }
           });

        // stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue = Volley.newRequestQueue(getApplicationContext());
        queue.add(stringRequest);
    }
}

ProductViewAdapter.java:

public class ProductViewAdapter extends ArrayAdapter {
    private Context context;
    private int layoutResourceId;
    private ArrayList<Product> data = new ArrayList();

    public ProductViewAdapter(Context context, int layoutResourceId, ArrayList data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.id = (TextView) row.findViewById(R.id.id);
            holder.first_name = (TextView) row.findViewById(R.id.firstname);
            holder.last_name = (TextView) row.findViewById(R.id.lastname);
            holder.email = (TextView) row.findViewById(R.id.email);
            holder.avatar = (ImageView) row.findViewById(R.id.image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        //int loader = R.drawable.loader;
        // Imageview to show
        Product Productitem = data.get(position);
        holder.id.setText(Productitem.getId());
        holder.first_name.setText(Productitem.getFirst_name());
        holder.first_name.setText(Productitem.getLast_name());
        holder.email.setText(Productitem.getEmail());
        String image_url = Productitem.getAvatar();
        Picasso.with(this.context)
            .load(image_url)
            .into(holder.avatar);
        return row;
    }

    static class ViewHolder {
        public TextView id;
        public TextView first_name;
        public TextView last_name;
        public TextView email;
        public ImageView avatar;
    }
}

Product.java:

public class Product implements Serializable {
    private int id;
    private String email;
    private String first_name;
    private String last_name;
    private String avatar;

    public Product(int id, String email, String first_name, String last_name, String avatar) {
        this.id = id;
        this.email = email;
        this.first_name = first_name;
        this.last_name = last_name;
        this.avatar = avatar;
    }

    public int getId() {
        return id;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
}

product_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:orientation="vertical"
        android:padding="5dp"
        android:paddingTop="20dp">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="1dp"
            android:adjustViewBounds="true"/>
        <TextView
            android:id="@+id/firstname"
            android:layout_width="match_parent"
            android:textAllCaps="false"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAlignment="center"
            android:gravity="center"
            android:textSize="25sp"/>
        <TextView
            android:id="@+id/lastname"
            android:layout_width="match_parent"
            android:textAllCaps="false"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAlignment="center"
            android:gravity="center"
            android:textSize="25sp"/>
        <TextView
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:textAllCaps="false"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAlignment="center"
            android:gravity="center"/>
        <!-- we keep the product id invisible, this if for reference only -->
        <TextView
            android:id="@+id/id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:gravity="center"
            android:textSize="0dp"
            android:visibility="invisible"/>
        </LinearLayout>

在我添加的依赖项下面:

implementation "com.squareup.picasso:picasso:2.4.0"
//implementation 'com.android.volley:volley:2.0.0'
implementation 'com.mcxiaoke.volley:library:1.0.19'

我已在清单文件中授予以下权限:

<uses-permission android:name="android.permission.INTERNET"/>

我无法使用凌空库通过URL在Android应用程序的Gridview中显示JSON数据。我得到:com.android.volley.NoConnectionError:java.io.IOException JSON数据来自https://reqres.in/api / ...

arrays json gridview android-volley
1个回答
0
投票

JSONObject results = jsonObject.getJSONObject("results");在您提到的链接中看不到任何对象结果。尝试删除此行。

© www.soinside.com 2019 - 2024. All rights reserved.