没有从android中的json数组获取值?

时间:2017-01-31 13:18:38

标签: android json gridview

我从Json Array获取值并将其传递给GridView,但它没有从json中获取值到gridview ..我知道代码中缺少某些内容...作为一个新手我不知道知道缺少什么?

这是一个代码:

 private void getData() {
        //Showing a progress dialog while our app fetches the data from url
        final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
        String DATA_URL = "http://........nList";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Toast.makeText(PMPigeonListingActivity.this,response,Toast.LENGTH_LONG).show();
                        loading.dismiss();
                        try {
                            JSONArray json = new JSONArray(response);
                            for (int i = 0; i < json.length(); i++) {
                                //Creating a json object of the current index
                                JSONObject obj = null;
                                try {
                                    //getting json object from current index
                                    obj = json.getJSONObject(i);
                                    //getting image url and title from json object
                                    pid.add(obj.getInt(String.valueOf(TAG_PID)));
                                    pname.add(obj.getString(TAG_PNAME));
                                    pdetails.add(obj.getString(TAG_PDETAILS));
                                    pmobile.add(obj.getString(TAG_MOBILE));
                                    pemail.add(obj.getString(TAG_EMAIL));
                                    images.add(obj.getString(TAG_IMAGE_URL));
                                    names.add(obj.getString(TAG_NAME));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        //Creating GridViewAdapter Object
                        PMPigeonListAdapter pmpigeonlistadapter = new PMPigeonListAdapter(getApplicationContext(), images, names, pid, pdetails, pmobile, pemail, pname);

                        //Adding adapter to gridview
                        pmpigeonlistadapter.notifyDataSetChanged();
                        gridView.setAdapter(pmpigeonlistadapter);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //Toast.makeText(PMPigeonListingActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("country", PostCountry);
                params.put("strain", PostStrain);
                params.put("distance", PostDistance);
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);


    }

我的xml:

 <GridView
                android:id="@+id/gridView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnWidth="90dp"
                android:divider="@color/list_divider"
                android:gravity="center"
                android:horizontalSpacing="1dp"
                android:numColumns="3"
                android:stretchMode="columnWidth"
                android:verticalSpacing="1dp"></GridView>
        </LinearLayout>

json输出::

{
  "status_code": 200,
  "status": "OK",
  "status_message": "Success",
  "pigeon_list": [
    {
      "id": "1",
      "pigeon_name": "sofiee",
      "auth_token": "58809c7129a5a",
      "country_code": "AE",
      "strain_id": "75",
      "distance": "3",
      "pigeon_price": "50.00",
      "pigeon_details": "One of the best ",
      "image": "http:.98a8ac5.jpeg",
      "pedigree_image": "http://...1.jpeg",...
      "status": "",
      "created": "2017-01-19 16:52:14",
      "updated": "0000-00-00 00:00:00",
      "strain_name": "Janssen/gaston wowers ",
      "usr_mobile": "+971/505040009",
      "usr_image": "http://....19a.jpeg",
      "usr_email": "...edo@gmail.com"
    },

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码更新代码:

PMPigeonListAdapter pmpigeonlistadapter = new PMPigeonListAdapter(MainActivity.this, images, names, pid, pdetails, pmobile, pemail, pname);