在android按钮上调用方法单击

时间:2015-07-13 12:53:28

标签: android button methods call

我正在尝试通过Android按钮点击进行Web服务调用。我设法纠正了错误,但它显示了获得响应的一些错误。我收到null回复。以下是我的代码。有人可以调试我..!

我的代码:

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

2 个答案:

答案 0 :(得分:2)

您是否收到编译时错误或运行时。您似乎正在尝试从具有返回类型void的方法返回一个包,即

.toObject()

答案 1 :(得分:0)

发现错误..!错误是,我已将AuthToken作为默认字符串传递。现在我宣布它为&#34; null&#34;在final中,即在onCreate之前,并从ClickEvent中删除它。它的完成..正确的代码如下..

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