1

I am trying to make a web service call via android button click. I've managed to rectify the errors, but it shows some bugs on getting response. I receive null response. Below is my code. Could someone debug me please..!

My code:

refresh_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            HttpHandler httpHandler1 = new HttpHandler();
            String res = null;
            String authToken = "MlSyULrWlFgVk28";

            try {

                Log.d("edwLog", TAG + " get_payment_notifications " + HttpHandler.API_URL + "get_payment_notifications/" + authToken + "/");
                res = httpHandler1.makeServiceCall(HttpHandler.API_URL + "get_payment_notifications/" + authToken + "/", HttpHandler.GET);
                Log.d("edwLog", TAG + " response > " + res);
                if (res != null) {
                    JSONObject jsonObject = new JSONObject(res);
                    String responseType = jsonObject.getString("type");
                    if (responseType.equals("success")) {
                        if (jsonObject.has("response")) {

                            JSONArray jsonArray = jsonObject.getJSONArray("response");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                notifications.add(jsonArray.getJSONObject(i));
                            }
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                Log.d("edwLog", TAG + " IOException > " + e.toString());
            }
        }
    });
piet.t
  • 11,718
  • 21
  • 43
  • 52
vss
  • 1,093
  • 1
  • 20
  • 33

2 Answers2

2

Are you getting compile time error or runtime. It seems that you are trying to return a bundle from a method that has a return type void i.e.

    public void onClick(View v)
Bhushan
  • 205
  • 2
  • 14
  • So what should i exactly do Bhushan? Could u pls edit the code for me? Below given are my necessary responses: ================================= Possible Responses: Incorrect token: {"type":"error","msg":"Security Token Missing."} No payment notifications: {"type":"error","msg":"No payment notifications"} Success: {"type":"success","response":{[notification array]}} – vss Jul 13 '15 at 13:06
  • 1
    Instead of returning something you can perform the operation that you need in that block itself. Also what is the use of the bundle object that you are trying to create. Because it seems to be just a new object without any data in it. – Bhushan Jul 13 '15 at 13:14
1

Found the bug..! The mistake was, I had passed the AuthToken as a default String. Now I declared it as "null" in final, i.e., before onCreate and deleted it from inside the ClickEvent. And its done.. The right code below..

refresh_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            HttpHandler httpHandler1 = new HttpHandler();
            String res = null;


            try {

                Log.d("edwLog", TAG + " get_payment_notifications " + HttpHandler.API_URL + "get_payment_notifications/" + AuthToken + "/");
                res = httpHandler1.makeServiceCall(HttpHandler.API_URL + "get_payment_notifications/" + AuthToken + "/", HttpHandler.GET);
                Log.d("edwLog", TAG + " response > " + res);
                if (res != null) {
                    JSONObject jsonObject = new JSONObject(res);
                    String responseType = jsonObject.getString("type");
                    if (responseType.equals("success")) {
                        if (jsonObject.has("response")) {

                            JSONArray jsonArray = jsonObject.getJSONArray("response");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                notifications.add(jsonArray.getJSONObject(i));
                            }
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                Log.d("edwLog", TAG + " IOException > " + e.toString());
            }
        }
    });
vss
  • 1,093
  • 1
  • 20
  • 33