Volley在使用JsonArrayRequest时不发送参数

时间:2016-10-27 20:06:20

标签: android arrays android-volley

使用JsonArrayRequest时,Android代码不会发送参数。

如果我使用StringRequest而不是JsonArrayRequest,如何将响应从String转换为JSONObject以在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);

这是从服务器发送的结果:

[
  {
    "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"
  }
]

1 个答案:

答案 0 :(得分:0)

您确定没有提供用户ID吗?在Volley请求完成之前,您有一个空的RecyclerView。你的Log.i陈述发生在Volley完成之前,所以你会看到一个空列表......

adapter.notifyDataSetChanged()循环后onResponse调用中需要while告诉适配器显示您添加的数据。

不太确定View在这里是什么......但你可能在两个视图上不需要相同的适配器。

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

如果我尝试使用StringRequest而不是JsonArrayRequest,我将如何将字符串响应从String转换为JSONObject以在RecyclerView中显示它们

您拥有String response onResponse,因此请使用JSONArray的{​​{1}}构造函数。

String

注意:这需要try-catch。