在 android 按钮点击时调用方法

Calling a method on android button click

我正在尝试通过单击 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());
            }
        }
    });

您遇到编译时错误还是运行时错误。似乎您正在尝试从具有 return 类型 void 的方法中 return 一个包,即

    public void onClick(View v)

发现错误..!错误是,我将 AuthToken 作为默认字符串传递。现在我在 final 中将其声明为 "null",即在 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());
            }
        }
    });