-1

The Android code is not sending the parameters when using JsonArrayRequest.

If I use StringRequest instead of JsonArrayRequest how do I convert the response from String to JSONObject to display it in a RecyclerView?

Intent intent = getIntent();
final String userID = intent.getExtras().getString("userID");
recyclerView = (RecyclerView)findViewById(R.id.display_expenses_recycler_view_id);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, expensesDisplay_url, (String) null,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                int count = 0;
                Log.i("Response", String.valueOf(response));
                while (count < response.length()) {
                    try {
                        JSONObject jsonObject = response.getJSONObject(count);
                        ExpensesDetails expensesDetails = new ExpensesDetails(jsonObject.getString("Expenses"),
                                jsonObject.getString("Description"), jsonObject.getString("datetime"));
                        arrayList.add(expensesDetails);
                        count++;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(DisplayExpenses.this, "Error", Toast.LENGTH_LONG).show();
                error.printStackTrace();
            }
        }) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("userID", userID);
        return params;
    }

};
Log.i("Array", String.valueOf(arrayList));
MySingleton.getMyInstance(DisplayExpenses.this).addToRequestQueue(jsonArrayRequest);
adapter = new RecyclerAdapterExpenses(arrayList);
recyclerView.setAdapter(adapter);View.setAdapter(adapter);

This is the result sent from the server:

[
  {
    "Expenses":"0",
    "Description":"car",
    "datetime":"2016-10-25 21:10:57"
  },
  {
    "Expenses":"2000",
    "Description":"Car",
    "datetime":"2016-10-25 21:46:05"
  },
  {
    "Expenses":"5000",
    "Description":"House payment",
    "datetime":"2016-10-25 21:47:11"
  },
  {
    "Expenses":"200",
    "Description":"",
    "datetime":"2016-10-26 20:51:42"
  },
  {
    "Expenses":"500",
    "Description":"",
    "datetime":"2016-10-26 23:55:21"
  }
]
Leo Nikkilä
  • 1,547
  • 18
  • 29
  • Look at the documentation for what should be at `(String) null`. **That** is the data you are to POST – OneCricketeer Oct 27 '16 at 20:17
  • JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, expensesDisplay_url, userID, new Response.Listener() { like this ?? – Jean Jacques Oct 27 '16 at 20:21
  • I am not too sure that a String is that parameter. According to this post, it's a `JSONObject`. http://stackoverflow.com/questions/18048806/volley-sending-a-post-request-using-jsonarrayrequest#18052417 – OneCricketeer Oct 27 '16 at 20:23
  • if i choose to use StringRequest how will i convert the string response into JSONArray to display it in the recycler view ?? – Jean Jacques Oct 27 '16 at 20:28
  • I'm not sure why you want to do that, but I just updated my answer below – OneCricketeer Oct 27 '16 at 20:28
  • Worth mentioning: Retrofit+Gson may be better suited than Volley for your problem – OneCricketeer Oct 27 '16 at 20:30

1 Answers1

0

Are you sure the user ID isn't being given? You have an empty RecyclerView until the Volley request finishes. And your Log.i statement happens before Volley completes, so you'll see an empty list...

You need an adapter.notifyDataSetChanged() within the onResponse call after the while loop to tell the adapter to display the data you added.

Not too sure what View is here... but you likely don't need the same adapter on two views.

recyclerView.setAdapter(adapter);View.setAdapter(adapter);

if i tried to use StringRequest instead of JsonArrayRequest how will i convert the string response from String to JSONObject to display them in a RecyclerView

You have a String response from onResponse, so use the constructor for JSONArray that takes a String.

JSONArray array = new JSONArray(response);

Note: that requires a try-catch.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245