将具有对象数组的JSON对象添加到列表视图

时间:2015-10-12 12:45:00

标签: android json android-layout listview android-volley

我有以下JSON:

{
    "user_wallets": [
        {
            "user_id": "56",
            "wallet_id": "25",
            "wallet_name": "Dandora Youth Voucher",
            "balance": "1,150.00"
        },
        {
            "user_id": "56",
            "wallet_id": "36",
            "wallet_name": "Pfizer Chama",
            "balance": "0.00"
        },
        {
            "user_id": "56",
            "wallet_id": "37",
            "wallet_name": "Sunshine",
            "balance": "1,000.00"
        }
    ]
}

我想将wallet_namebalance添加到列表视图中,如下所示:

wallet_name       balance
wallet_name       balance
wallet_name       balance

等等。但是,我使用volley库对我的api进行http调用,但响应显示并清空了Toast消息。

我哪里可以错?

我正在打电话:

public void getMyWallets(){
    final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try{
                JSONArray user_wallets = response.getJSONArray("user_wallets");

                for(int i = 0; i<user_wallets.length();i++){
                    JSONObject wallet = user_wallets.getJSONObject(i);

                    WalletModel walletModel = new WalletModel();
                    walletModel.setWallet_name(wallet.getString("wallet_name"));
                    walletModel.setBalance(((Number) wallet.get("balance")).doubleValue());

                    walletModelList.add(walletModel);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            walletListAdapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            hidepDialog();
        }

    });
    MyApplication.getInstance().addToRequestQueue(jsonObjReq);


}

2 个答案:

答案 0 :(得分:0)

networkResponse中有一个VolleyError引用,请查看它,看看是否有任何有用的信息,以便了解错误的位置。

这样的事情:

@Override
public void onErrorResponse(VolleyError error) {
    if(error.networkResponse.data!=null) {
        try {
            body = new String(error.networkResponse.data,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

所以我发现代码没有问题。问题是凌空请求超时问题: 查看 Volley RequestQueue Timeouthttps://stackoverflow.com/questions/17094718/android-volley-timeout?rq=1。我通过这样做解决了我的空列表问题:

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());

MyApplication.getInstance().addToRequestQueue(jsonObjReq);
    int socketTimeout = 15000;//30 seconds - change to what you want
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    jsonObjReq.setRetryPolicy(policy);
    mRequestQueue.add(jsonObjReq);

为要获取的数据添加了更多请求超时:)