我试图在RecyclerView中显示数据,但数据没有显示

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

我试图在RecyclerView中显示数据,但数据没有显示,错误是

02-28 18:45:49.424 8426-8426/com.bbcoin.saksham.bbcoin W/System.err:at org.json.JSONArray.get(JSONArray.java:293)
    02-28 18:45:49.424 8426-8426/com.bbcoin.saksham.bbcoin W/System.err:at org.json.JSONArray.getJSONObject(JSONArray.java:521)

我的代码如下:

StringRequest stringRequest = new StringRequest( Request.Method.POST, json_url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                  //  Toast.makeText( Trans_List.this,response.length(),Toast.LENGTH_SHORT ).show();
                    int count = 0;
                   while (count<response.length()){
                        try {
                            JSONArray jsonArray = new JSONArray (response);
                            JSONObject jsonObject = jsonArray.getJSONObject( count );
                            Contact contact  = new Contact(
                                    jsonObject.getString( "transfer_by_wallet" ),
                                    jsonObject.getString( "dr" ),
                                    jsonObject.getString( "cr" ));
                            arrayList.add( contact );
                            count++;
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    adapter = new RecyclerAdapter( arrayList );
                    Log.d("arraylist size",""+arrayList.size());
                    recyclerView.setAdapter( adapter );
                }

            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText( Trans_List.this,"Check network connection...",Toast.LENGTH_SHORT ).show();
        }
    } )
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            User user = new User( Trans_List.this );
            String email = user.getEmail();
            Map<String, String > params = new HashMap<>(   );
            params.put( "email", email );
            return params;
        }
    };
    MySingleton.getInstance( Trans_List.this ).addTorequestque( stringRequest );

数据响应显示在android profiler中,但不显示在RecyclerView中。我需要帮助来解决这个错误,

android
1个回答
0
投票

JSONObject解析时出错。我用JsonArrayRequest更改了它并且还更改了@Override方法getParams()

protected Map<String, String> getParams() throws AuthFailureError,

与@Override getHeaders()

 public Map<String, String> getHeaders() throws AuthFailureError

修正后的代码如下

JsonArrayRequest jsonArrayRequest =   new JsonArrayRequest( Request.Method.POST, json_url,(String) null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    int count = 0;
                    while (count<response.length()){
                        try {

                            JSONObject jsonObject = response.getJSONObject( count );
                            ListProvider listProvider  = new ListProvider(
                                    jsonObject.getString( "transfer_by_wallet" ),
                                    jsonObject.getString( "dr" ),
                                    jsonObject.getString( "cr" ),
                                    jsonObject.getString( "trans_date"));
                            arrayList.add( listProvider );
                            count++;
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    adapter = new RecyclerAdapter1( arrayList );
                    Log.d("arraylist size",""+arrayList.size());
                    recyclerView.setAdapter( adapter );
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText( Transaction_List.this,"Error...",Toast.LENGTH_LONG ).show();
            error.printStackTrace();
        }
    }
    ){
             @Override
             public Map<String, String> getHeaders() throws AuthFailureError {
                 Map<String,String> headers = new HashMap<>(  );
                 headers.put( "email",email );
                 return headers;
             }


    };
    MySingleton.getInstance( Transaction_List.this ).addTorequestque( jsonArrayRequest );
© www.soinside.com 2019 - 2024. All rights reserved.