0

I have the following 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"
        }
    ]
}

I want to add the wallet_name and balance to a list view like this:

wallet_name       balance
wallet_name       balance
wallet_name       balance

and so on. However, I make the http call to my api using volley library but the response shows and empty Toast message.

Where can I be wrong?

The call I am making is this :

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


}
Rami
  • 7,663
  • 11
  • 32
  • 64
musale
  • 394
  • 7
  • 15
  • please show your log – Egos Zhang Oct 12 '15 at 13:08
  • Egos, I'm using Android Studio 1.4 and I can't seem to find the logs :| – musale Oct 12 '15 at 14:02
  • An established connection was aborted by the software in your host machine java.io.IOException: An established connection was aborted by the software in your host machine at sun.nio.ch.SocketDispatcher.write0(Native Method) at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51) at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:94) at sun.nio.ch.IOUtil.write(IOUtil.java:65) at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:450) at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) at ... [Long text] – musale Oct 12 '15 at 14:42

2 Answers2

0

There is a networkResponse reference in the VolleyError, check it out and see if you get any useful info in order to understand where is the bug.

Something like this:

@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();
        }
    }
}
denis_lor
  • 5,442
  • 4
  • 20
  • 46
  • I have tried that with Toast and it is empty. As for the logs I seem not to get them in AS 1.4 – musale Oct 12 '15 at 14:35
0

So I figured out that there was no problem with the code. The problem was a timeout issue with volley requests: Check out Volley RequestQueue Timeout and https://stackoverflow.com/questions/17094718/android-volley-timeout?rq=1 . I solved my empty list problem by doing this:

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

That added more request timeout for the data to be fetched :)

Community
  • 1
  • 1
musale
  • 394
  • 7
  • 15