【问题标题】:Parsing JSON Array in Recyclerview using volley, why it show less data than it have on server?使用 volley 在 Recyclerview 中解析 JSON 数组,为什么它显示的数据少于服务器上的数据?
【发布时间】:2018-05-13 01:23:45
【问题描述】:

我正在尝试在recyclerview 中使用volleyandroid 中解析Json Array,但它显示的数据少于服务器上的数据。 我的意思是它只解析 4 个数据,服务器有 10 个。为什么? 代码:

JsonObjectRequest jsonObjectReq = new JsonObjectRequest(Request.Method.GET, url, null,

                new Response.Listener<JSONObject>() {                
                @Override
                public void onResponse(JSONObject response) {

                    try {
                       JSONObject data, deal_mode;
    String total, ref, name,category_name,start_date,end_date,offer_price,market_price,gain,structure_note,other_note;

    data = resp.getJSONObject("data");
    total = data.getString("total");
    JSONArray data1 = data.getJSONArray("data");

    try {
        for (int i = 0; i <response.length(); i++) {           
            JSONObject jresponse = data1.getJSONObject(i);
            ref = jresponse.getString("ref");
            name = jresponse.getString("name");
            deal_mode = jresponse.getJSONObject("deal_mode");
            category_name=deal_mode.getString("category_name");
            start_date=jresponse.getString("start_date");
            end_date=jresponse.getString("end_date");
            offer_price=jresponse.getString("offer_price");
            market_price=jresponse.getString("market_price");
            gain=jresponse.getString("gain");
            structure_note=jresponse.getString("structure_note");
            other_note=jresponse.getString("other_note");

            Toast.makeText(getApplication(), name, Toast.LENGTH_LONG).show();          
        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // progressBar.setVisibility(View.INVISIBLE);
            Toast.makeText(getApplication(), error.toString(), Toast.LENGTH_LONG).show();
        }
    }) {
        //This is for Headers
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json; charset=UTF-8");
            params.put("Authorization", "Bearer" + getToken);
            return params;
        }
    };
    //Add the Request to the RequestQueue.
    VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectReq, REQUEST_TAG);

【问题讨论】:

  • 你的代码在哪里...???
  • Dinesh 请发布您的代码以便更好地参考

标签: android arrays json android-recyclerview android-volley


【解决方案1】:
 try {
    for (int i = 0; i <data1.length(); i++) {           
        JSONObject jresponse = data1.getJSONObject(i);
        ref = jresponse.getString("ref");
        name = jresponse.getString("name");
        deal_mode = jresponse.getJSONObject("deal_mode");
        category_name=deal_mode.getString("category_name");
        start_date=jresponse.getString("start_date");
        end_date=jresponse.getString("end_date");
        offer_price=jresponse.getString("offer_price");
        market_price=jresponse.getString("market_price");
        gain=jresponse.getString("gain");
        structure_note=jresponse.getString("structure_note");
        other_note=jresponse.getString("other_note");

        Toast.makeText(getApplication(), name, Toast.LENGTH_LONG).show();          
    }

您的代码中的错误是您只是将 for 循环迭代到 response.length 但 for 循环应该迭代到 data1.length 数组。 问题解决了干杯!!!

【讨论】:

  • 可能有一些错误,请查看堆栈跟踪并粘贴解析代码并链接到示例 json。那么我可以更好地帮助你。
  • 现在我的下一个数据在下一页,我的意思是每页 10 个数据。我在recyclerview中解析它,当我们向下滚动recyclerview时,我如何将下一个数据解析到我的recyclerview?
  • 请为答案投票,以便它可以帮助其他人寻找它。
  • 为此,您可以在 recyclerview 中使用分页,此链接将为您提供帮助。 medium.com/@etiennelawlor/…
猜你喜欢
  • 1970-01-01
  • 2021-04-13
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 2019-01-09
相关资源
最近更新 更多