GCM使用smack库NoResponseException:在回复超时内没有收到响应

时间:2015-06-25 13:21:09

标签: smack google-cloud-messaging

我是gcm的新手,我尝试使用Smack API连接到GCM的Cloud Connection Server(XMPP)。一开始没事, 我的代码:

uid = "123456789";
apiKey = "A**************B";
XMPPTCPConnectionConfiguration.Builder config;
config.setSocketFactory(SSLSocketFactory.getDefault());
    config = XMPPTCPConnectionConfiguration.builder();
    config.setUsernameAndPassword(uid,apiKey);
    config.setServiceName("gcm.googleapis.com");
    config.setHost("gcm.googleapis.com");
    config.setPort(5235);
    config.setDebuggerEnabled(true);
    mConnection = new XMPPTCPConnection(config.build());
    mConnection.setPacketReplyTimeout(10000);
    try {
         mConnection.connect();
         mConnection.login();
        }
   catch (SmackException | IOException | XMPPException e) {
            System.out.println("Exception at SmackCcsClient.init()");
            e.printStackTrace();
        }

但我无法通过最初的握手过程。我首先使用了一些虚拟随机GCMID来测试下游消息,它显示在smack调试窗口中,但稍后,相同的代码在以下xml提示后显示为原始发送数据包

<stream:stream xmlns='jabber:client' to='gcm.googleapis.com'  xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>

我试过

mConnection.login(uid+"@gcm.googleapis.com",apiKey);//even though i assume its next step of the handshake.

控制台会输出以下错误:

org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 10000ms (~10s). Used filter: No filter used or filter was 'null'.
at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:106)
at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:85)
at org.jivesoftware.smack.SynchronizationPoint.checkForResponse(SynchronizationPoint.java:253)
at org.jivesoftware.smack.SynchronizationPoint.checkIfSuccessOrWait(SynchronizationPoint.java:146)
at org.jivesoftware.smack.SynchronizationPoint.checkIfSuccessOrWaitOrThrow(SynchronizationPoint.java:125)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:837)
at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:360)
at psdc.gcm.SmackCcsClient.init(SmackCcsClient.java:64)
at psdc.gcm.GCMServer.activate(GCMServer.java:44)
at psdc.servlets.Mapper.selectIds(Mapper.java:191)
at psdc.servlets.Mapper.doPost(Mapper.java:152)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Jun 25, 2015 5:36:18 PM org.jivesoftware.smack.AbstractXMPPConnection callConnectionClosedOnErrorListener
WARNING: Connection closed with error
java.io.EOFException: input contained no data
    at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:2965)
    at org.xmlpull.mxp1.MXParser.more(MXParser.java:3003)
    at org.xmlpull.mxp1.MXParser.parseProlog(MXParser.java:1409)
    at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1394)
    at org.xmlpull.mxp1.MXParser.next(MXParser.java:1092)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1151)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
    at java.lang.Thread.run(Unknown Source)

请帮助我解决这个问题,因为我真的很困惑,无处可去。请告诉我一种方法来检查我的xml请求是否到达谷歌。

使用SMACK库版本4.1.1

1 个答案:

答案 0 :(得分:1)

使用config.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible);解决了我的问题。它定义了要使用的连接的配置。如果服务器支持相同的连接,则打开TLS。

参考:http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/ConnectionConfiguration.html

config = XMPPTCPConnectionConfiguration.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible);
        config.setSocketFactory(SSLSocketFactory.getDefault());
        config.setUsernameAndPassword(uid,apiKey);
        config.setServiceName("gcm.googleapis.com");
        config.setHost("gcm.googleapis.com");
        config.setPort(5235);
        config.setDebuggerEnabled(true);
            mConnection = new XMPPTCPConnection(config.build());
            mConnection.setPacketReplyTimeout(10000);

            try {
                mConnection.connect();

希望它有所帮助。