0

I am using a JsonObject request within onActivityCreated of my fragment. I am adding the jsonobject request to a request queue. The queue is not getting executed at all. I have put Log statement in Onresponse and Onerrorresponse. Its just like the request is not started at all. Below is my code. Please suggest if am doing something wrong. I need to pass a string to the server. So i am putting it in a jsonobject jlistname. Server will return a jsonobject back, so to receive it im using jsonobjectrequest instead of stringrequest.

RequestQueue rq;
JsonObjectRequest jsObjRequest;
JSONObject jlistname = new JSONObject();
String listname="list1";

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    searchitembtn=(ImageButton) getView().findViewById(R.id.searchitembtn);
    rq = Volley.newRequestQueue(getActivity());
    try {
        jlistname.put("listname", listname);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    jsObjRequest = new JsonObjectRequest(Request.Method.POST, getlistitems, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i(TAG, "im in onresponse" + response);
                    System.out.println(response);
                 }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i(TAG, "im in onerrorresponse" + error);
                }
            }) {
        @Override
        public byte[] getBody() {
            return jlistname.toString().getBytes();
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            return headers;
        }
    };


    if (listtype==100||listtype==101){
        -----------------------
        }
    else if(listtype==102){
        Log.i(TAG,"mylisttypeis102: "+listtype);
        Log.i(TAG,"im before adding o request queue"+jsObjRequest);
        rq.add(jsObjRequest);
        Log.i(TAG, "im after adding o request queue"+rq);
    }
    else {
       -------------------
        }

    }
     showLists();

}

UPDATE : I have added a button to the layout and in the onclicklistener of the button kept rq.add(jsObjRequest);

Then i am getting the response correctly.

But i really do not want the response on click. I want the response to be generated when the fragment is loaded as per my code in onActivityCreated.

Can someone explain why i get response only using a listener ?

user6005433
  • 56
  • 1
  • 5
  • Check `listtype`'s value please – BNK Mar 16 '16 at 01:32
  • Hi @BNK yes the list type is 102. All three Log statements within the else if condition are getting printed. – user6005433 Mar 17 '16 at 04:00
  • Any other logcat info available or not? – BNK Mar 17 '16 at 04:11
  • No there in no other info. Logs in onresponse, on error, or exception, nothing is there. Its as if, the request is never executed at all. – user6005433 Mar 17 '16 at 23:15
  • Ah, you did not pass the URL to the request. So that is the reason :) – BNK Mar 17 '16 at 23:54
  • getlistitems is my URL String – user6005433 Mar 18 '16 at 00:55
  • Re-run your app, then copy and post all your logcat information – BNK Mar 18 '16 at 01:17
  • 03-17 21:21:19.561 17264-17264/? I/swapna: mylisttypeis: 102 03-17 21:21:19.561 17264-17264/? I/swapna: im jlistname{"listname":"gurd"} 03-17 21:21:19.561 17264-17264/? I/swapna: mylisttypeis102: 102 03-17 21:21:19.561 17264-17264/? I/swapna: im before adding o request queue[ ] http://192.168.1.7/getlistitems.php 0x355f91b NORMAL null 03-17 21:21:19.561 17264-17264/? I/swapna: im after adding o request queuecom.android.volley.RequestQueue@1df9f7d9 – user6005433 Mar 18 '16 at 01:26
  • i changed listname to gurd from list1. Rest is same. – user6005433 Mar 18 '16 at 01:28
  • @BNK it is working if i move the rq.add(jsObjRequest); part into a listener. I have kept a button and this line in the onclicklistener of the button, then i got results. But when called directly in the onactivitycreated it is not working – user6005433 Mar 18 '16 at 01:44
  • Try moving your code into `onCreateView` instead of `onActivityCreated`, then check – BNK Mar 18 '16 at 01:56
  • @BNK Tried it. Moved code to onCreateView. Still same , the LOGs in Elseif part are printed but nothing from respose. – user6005433 Mar 18 '16 at 03:46
  • Try setting break points at `Log.i...` inside `onResponse` and `onErrorResponse` then start debugging (Shift+F9) to check if one of them called or not – BNK Mar 18 '16 at 04:03
  • For some reason AVD on my machine doesnt start. S im using my samsung galaxy to test my app and capture the logcat. I dont know how to debug using break points. – user6005433 Mar 21 '16 at 22:22
  • FYI http://developer.android.com/tools/debugging/debugging-studio.html – BNK Mar 22 '16 at 01:15

1 Answers1

0

Add the request to the RequestQueue

rq.add(jsonObject);
bGorle
  • 1,978
  • 2
  • 22
  • 28
  • I did that in the else if part : else if(listtype==102){ Log.i(TAG,"mylisttypeis102: "+listtype); Log.i(TAG,"im before adding o request queue"+jsObjRequest); rq.add(jsObjRequest); Log.i(TAG, "im after adding o request queue"+rq); } – user6005433 Mar 16 '16 at 01:39
  • Check that listvalue input – bGorle Mar 16 '16 at 01:41
  • I am adding the request to the requestqueue in the elseif part. – user6005433 Mar 16 '16 at 01:42
  • I Got that, `listtype == 102` then only it will be added to requestqueue. otherwise it won't – bGorle Mar 16 '16 at 01:43
  • i am getting the all three log statements in the else if part. The listtype is 102 and that else if part is getting executed.
    03-15 21:07:32.051 21206-21206/? I/swapna: mylisttypeis102: 102 03-15 21:07:32.051 21206-21206/? I/swapna: im before adding o request queue[ ] http://192.168.1.7/getlistitems.php 0x355f91b NORMAL null 03-15 21:07:32.051 21206-21206/? I/swapna: im after adding o request queuecom.android.volley.RequestQueue@2fb8c04c
    – user6005433 Mar 16 '16 at 01:43
  • See this http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working#comment-37747300 – bGorle Mar 16 '16 at 02:00
  • Yeah, i saw this one. After seeing it i removed getparams and kept getbody. – user6005433 Mar 16 '16 at 02:04
  • Remove the getBodyContenttype, There your giving type as json and sending bytes – bGorle Mar 16 '16 at 02:05
  • I have taken out getBodyContenttype block . Even now im not seeing anything. – user6005433 Mar 16 '16 at 02:09