简体   繁体   English

使用JSON Volley获取数据

[英]fetching data using json volley

I am fetching data from db and want to set text. 我正在从数据库获取数据,并希望设置文本。 Values are getting fetched and displaying in url but setText not working, when I used JSONObject also same problem. 值被获取并显示在url中,但是setText不起作用,当我使用JSONObject时也遇到了同样的问题。 Where am I going wrong.? 我要去哪里错了? Below is my code and backend output. 下面是我的代码和后端输出。

{
"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);
}

Change your try block contents like below 如下更改您的try块内容

            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();
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM