【问题标题】:get empty notification in production server在生产服务器中获得空通知
【发布时间】:2014-02-13 13:02:04
【问题描述】:

我使用镜像 api 开发了一个谷歌玻璃应用程序。在开发过程中,我使用 "Introspected tunnels to localhost" 来接收通知。

现在我在生产服务器上上传了我的应用程序。所以现在我将我的回调 URL 配置为我的域名,例如 https://www.mydomain.com:8443/notify。但我收到空通知。

在通知 servlet 中:

BufferedReader notificationReader = new BufferedReader(
        new InputStreamReader(request.getInputStream()));
String notificationString = ""; 
int lines = 0;

while (notificationReader.ready()) {
    notificationString += notificationReader.readLine();
    lines++;

    if (lines > 1000) {
        throw new IOException(
                "Attempted to parse notification payload that was unexpectedly long.");
    }
}

LOG.info("\ngot raw notification : " + notificationString);

在 catalina.out 中

Feb 13, 2014 12:51:48 PM com.google.glassware.NotifyServlet doPost
INFO: got raw notification : 

我该如何解决?

【问题讨论】:

  • 听起来你有一个需要隔离的小鬼。如果您使用 curl 将某些内容发布到通知端点会发生什么?您发布的内容是否会出现在您的日志中?
  • 是的,这是我的日志,它是空的。不,我还没有使用 curl。我应该隔离哪一部分?
  • Rudy - 我认为 Jenny 建议您使用 curl 来模拟您希望从客户端应用程序获得的通知。这有意义吗?
  • 是的,我正在努力。
  • 你的网站通知地址和调用示例是什么?

标签: java google-glass google-mirror-api


【解决方案1】:
StringBuffer stringBuffer = new StringBuffer();
                String line = "";
                BufferedReader bufferReader = new BufferedReader(
                                       new InputStreamReader(request.getInputStream()));
                while ((line = bufferReader.readLine()) != null) {         
                    stringBuffer.append(line);
                }
                notificationString = stringBuffer.toString();

希望它会起作用。

【讨论】:

    【解决方案2】:

    我认为您应该使用readLine() 方法。stack overflow 的答案之一建议不要使用 ready() 来满足此类要求。

    ready 方法告诉我们 Stream 是否准备好被读取。 想象一下您的流正在从网络套接字读取数据。在这 情况下,流可能还没有结束,因为套接字还没有结束 关闭,但它可能还没有为下一个数据块做好准备,因为 套接字的另一端没有再推送任何数据。

    在上述场景中,我们无法读取更多数据,直到远程 end 推送它,所以我们必须等待数据可用,或者 用于关闭套接字。 ready() 方法告诉我们什么时候数据 可用。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我将代码更改为如下所示:

       StringBuffer jb = new StringBuffer();
       String notificationString = "";
       String line = "";
       BufferedReader reader = request.getReader();
       while ((line = reader.readLine()) != null) {         
           jb.append(line);
       }
       notificationString = jb.toString();
      

      【讨论】:

      • 你快到了。只需要改变BufferedReader,比如BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));然后它会工作
      【解决方案4】:

      试试这个:

      while(notificationReader.ready()) {
          notificationString = notificationString.concat(notificationReader.readLine());
          lines++;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-24
        • 1970-01-01
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多