3

正如许多其他 java 和 android 新手一样,我正在尝试在 windows 上运行 gcm 演示服务器和 gcm 演示客户端,使用 eclipse 运行 vertual 设备和 tomcat 作为 webserer。

我在注册和取消注册设备方面没有问题,但在尝试发送消息时出现错误 401。阅读了很多关于类似问题的答案,但仍然不知道我做错了什么。我将 apy 密钥、服务器 url 和发件人 ID 放在教程中提到的位置。还稍微更改了 SendAllMessagesServlet.java 中的代码:

// Processes the request to add a new message.  
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws IOException, ServletException {
    List<String> devices = Datastore.getDevices();
    String status;
    if (devices.isEmpty()) {
        status = "Message ignored as there is no device registered!";
    } else {
        // NOTE: check below is for demonstration purposes; a real application
        // could always send a multicast, even for just one recipient
        // MY CODE
        String collapseKey = "mycollapsekey";
        String userMessage = "testmessage";
        // END OF MY CODE
        if (devices.size() == 1) {
            // send a single message using plain post
            String registrationId = devices.get(0);
            //  Message message = new Message.Builder().build();
            //  BELOW I HAVE CHANGED MESSAGE BUILDING A BIT:
            Message message = new Message.Builder()
                .collapseKey(collapseKey)
                .timeToLive(30)
                .delayWhileIdle(true)
                .addData("message", userMessage)
                .build();
            Result result = sender.send(message, registrationId, 5);
            status = "Sent message to one device: " + result;
        } else {
            // send a multicast message using JSON
            // must split in chunks of 1000 devices (GCM limit)
            int total = devices.size();
            List<String> partialDevices = new ArrayList<String>(total);
            int counter = 0;
            int tasks = 0;
            for (String device : devices) {
                counter++;
                partialDevices.add(device);
                int partialSize = partialDevices.size();
                if (partialSize == MULTICAST_SIZE || counter == total) {
                    asyncSend(partialDevices);
                    partialDevices.clear();
                    tasks++;
                }
            }
            status = "Asynchronously sending " + tasks + " multicast messages to " + total + " devices";
        }
    }
    req.setAttribute(HomeServlet.ATTRIBUTE_STATUS, status.toString());
    getServletContext().getRequestDispatcher("/home").forward(req, resp);
}

但是发送消息时我仍然收到下一个错误,这是我的 tomcat localhost 日志文件:

окт 22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log
INFO: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL: http://jakarta.apache.org]]
окт 22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
окт 22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
окт 22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
окт 22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
окт 22, 2012 1:07:05 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet SendAllMessagesServlet threw exception
com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401
    at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:177)
    at com.google.android.gcm.server.Sender.send(Sender.java:121)
    at com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServlet.java:93)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:881)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:674)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:541)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)

标准错误日志:

2012-10-22 13:06:37 Commons Daemon procrun stderr initialized
окт 22, 2012 1:06:38 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Android\android-sdk\tools;C:\Program Files\Android\android-sdk\platform-tools;c:\ant\bin;C:\Program Files\Java\jdk1.7.0_07\lib;;.
окт 22, 2012 1:06:38 PM org.apache.coyote.http11.Http11BaseProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
окт 22, 2012 1:06:38 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 531 ms
окт 22, 2012 1:06:38 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
окт 22, 2012 1:06:38 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.5.36
окт 22, 2012 1:06:38 PM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
окт 22, 2012 1:06:39 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive gcm-demo.war
окт 22, 2012 1:06:39 PM com.google.android.gcm.demo.server.ApiKeyInitializer contextInitialized
INFO: Reading /api.key from resources (probably from WEB-INF/classes
окт 22, 2012 1:06:39 PM org.apache.coyote.http11.Http11BaseProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
окт 22, 2012 1:06:39 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
окт 22, 2012 1:06:39 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/32  config=null
окт 22, 2012 1:06:39 PM org.apache.catalina.storeconfig.StoreLoader load
INFO: Find registry server-registry.xml at classpath resource
окт 22, 2012 1:06:39 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1219 ms
окт 22, 2012 1:06:58 PM com.google.android.gcm.demo.server.Datastore register
INFO: Registering APA91bF0kArKvvJsHu4qS92FQl-CLKt0CJL8_SrtyL9OkGSqQQUMywMvPhulG4xmdiX_uVhdXs6xERY5pwQzHES-aI-tBCOgMZ_JOBT8S_RIK9XhyQDf49Yfr9g4nSnfgN2GIJHlnqTA4mAJBolnjfuQKLmULl0g5g

我错过了什么或在哪里可以阅读更多关于解决此类问题的信息?

4

4 回答 4

2

我刚刚回答了一个类似的问题——访问/gcm-demo/sendAll。原因:HTTP 状态码:401

检查您正在使用的发件人 API 密钥。

于 2012-10-22T11:22:06.573 回答
1

我也遇到了这个错误

在此处输入图像描述

原来我没有启用Google Cloud Messaging for Android。(在我的一台机器上,这是按照相同的步骤自动启用的,不知道为什么在另一台机器上没有这样做)长话短说,转到开发人员控制台并启用它。

APIs and Auths | APIs | Google Cloud Messaging for Android

刷新您的浏览器,然后重试。

于 2015-02-27T15:46:22.230 回答
1

这个问题在任何论坛上都没有得到很好的解决,可能是因为它有多种原因。我的情况是:我正在做一个 GCM 推送通知的演示,它第一次就起作用了。但是当我第二天继续使用相同的代码时,我发现答案中提到了异常。经过观察,我注意到我的公共 IP 已更改,因为它不是静态的。在谷歌开发者控制台上创建项目时必须提供此 IP。您可以通过在谷歌中搜索“我的公共 IP 地址”来找到您的公共 IP。或者出于演示目的,我现在强烈建议您使用 IP 作为推荐的 0.0.0.0/0,并且当您部署项目时,您的服务器 IP 将是静态的。然后,您可以将 0.0.0.0/0 IP 替换为您的服务器 IP。此外,请检查所有密钥,例如“项目编号”和“服务器 API 密钥”。希望能帮助到你。干杯:)

于 2016-01-28T08:30:44.717 回答
0

Commons Daemon procrun stderr 已初始化

如果有人遇到上述错误,请尝试检查您的 jvm.dll 路径默认路径位于 *C:\Program Files\Java\jdk1.6.0_37\jre\bin\client\jvm.dll* 将其更改为正确的路径。

于 2012-11-12T12:35:24.727 回答