【问题标题】:Calling a method on android button click在android按钮单击上调用方法
【发布时间】:2015-10-01 18:03:24
【问题描述】:

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

【问题讨论】:

  • logcat 日志在哪里?
  • 这到底是什么? :final Bundle result = new Bundle(); return result; 结果什么都不是!
  • 是的,这就是我的打击 Skizo..
  • 应该返回什么结果?
  • @vinaysam 这不是 Logcat 错误日志 :(

标签: android button methods call


【解决方案1】:

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

    public void onClick(View v)

【讨论】:

  • 那么我到底应该怎么做 Bhushan?你能为我编辑代码吗?下面给出了我必要的回应:================================== 可能的回应:不正确的令牌:{“type” :"error","msg":"Security Token Missing."} 没有支付通知:{"type":"error","msg":"没有支付通知"} 成功:{"type":"success", “响应”:{[通知数组]}}
  • 您可以在该块本身中执行您需要的操作,而不是返回一些东西。另外,您尝试创建的捆绑对象有什么用途。因为它似乎只是一个没有任何数据的新对象。
【解决方案2】:

发现错误..!错误是,我将 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());
            }
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2019-02-28
    • 1970-01-01
    • 2017-01-25
    相关资源
    最近更新 更多