0

我从 API 作为 JSONarray 获得响应,我可以将数据放入我的 arraymodel,但问题是我需要来自一个数组的一些特定数据,我不知道如何做到这一点。

数组是:

"history": [
        {
            "id": "20",
            "code": "mcw_5b97de588ce0c",
            "date": "2018-09-11 17:25:12",
            "status": "1",
            "name": "a:1:{s:7:\"english\";s:9:\"rent bill\";}",
            "img": "shop.png"
        }

我想要名称中的英语、S:9 和租金账单数据。

我在这里发布我的代码,以便您了解我做了什么以及我犯了什么错误。

public void  shopListing()
{

    showSimpleProgressDialog(this, "Loading...","Fetching Shops History",false);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, shopurl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("strrrrr", ">>" + response);

                    try {

                        JSONObject obj = new JSONObject(response);
                        //if(obj.optString("status").equals("true")){

                        dataModelArrayList = new ArrayList<>();
                        JSONArray dataArray  = obj.getJSONArray("history");

                        for (int i = 0; i < dataArray.length(); i++) {

                            shopModel playerModel = new shopModel();
                            JSONObject dataobj = dataArray.getJSONObject(i);
                            playerModel.setId(dataobj.getString("id"));
                            playerModel.setName(dataobj.getString("name"));
                            playerModel.setDetails(dataobj.getString("date"));


                            dataModelArrayList.add(playerModel);

                        }

                        setupListview();

                        // }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            String api = getApi_key();
            String user = getUser_key();

            Map<String, String> param = new HashMap<> ();
            param.put("api_key", api);
            param.put("user_key", user);


            return param;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(stringRequest);

}
4

1 回答 1

0

您的服务器响应不好。与其将名称作为字符串发送给您,不如将“名称”作为对象返回,如下所示:

"history": [
    {
        "id": "20",
        "code": "mcw_5b97de588ce0c",
        "date": "2018-09-11 17:25:12",
        "status": "1",
        "name": 
            {
                "s":7,
                "subject":"english",
                "s":9,
                "any_key":"rent bill"
            },
        "img": "shop.png"
    }

我觉得服务器正在返回您为处理来自客户端的请求而编写的代码字符串。请仔细检查。

于 2019-03-26T04:50:42.180 回答