使用json volley获取数据

时间:2017-12-26 07:12:38

标签: android arrays json android-volley settext

我从db获取数据并想要设置文本。获取值并在url中显示但setText不起作用,当我使用JSONObject时也存在同样的问题。我哪里错了。?下面是我的代码和后端输出。

{
"status": 200,
"db": {
    "test_count": 2539,
    "franchise_count": 2,
    "patient_count": 1,
    "invoice_count": 1,
    "total_income": "12140",
    "current_income": "12140",
    "total_expense": null,
    "current_expense": null,
    "user_count": 2
}}

 JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    progressDialog.dismiss();
                    try {
                                JSONObject object = new JSONObject();
                                u = object.getString("user_count");
                                user_count.setText(u);

                    } catch (JSONException e) {
                        Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
                        Log.e("Error", "Failed" +e.toString());
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Log.e("Error", "Try Later" +error.toString());
                    Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(objectRequest);
}

1 个答案:

答案 0 :(得分:1)

更改下面的试用块内容

            new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                progressDialog.dismiss();
                try {
                            JSONObject newObject=response.getJSONObject("db"); 
                            u = newObject.getString("user_count");
                            user_count.setText(u);

                } catch (JSONException e) {
                    Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
                    Log.e("Error", "Failed" +e.toString());
                    e.printStackTrace();
                }
            }