2012-10-22 106 views
0

我收到以下錯誤,當我試圖發送一個消息:訪問/ gcm-demo/sendAll。原因:HTTP狀態代碼:401

HTTP ERROR 500 

Problem accessing /gcm-demo/sendAll. Reason: 

HTTP Status Code: 401 
Caused by: 

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:82) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) 
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390) 
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) 
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) 
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766) 
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) 
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) 
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) 
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) 
at org.mortbay.jetty.Server.handle(Server.java:326) 
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) 
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926) 
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) 
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) 
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) 
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410) 
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) 
Powered by Jetty:// 

我使用的是Android示例項目所講述的位置:http://developer.android.com/guide/google/gcm/demo.html

有人可以幫助我出來嗎?這個錯誤來自哪裏?

我在Linux機器上使用Jetty和它編譯採用Apache Ant在Windows計算機上一個.war ...

回答

0

發送方可能不會與谷歌的授權正確。

Sender sender = new Sender(MY_KEY);

是MY_KEY你與谷歌註冊了相同的密鑰?替換API密鑰,如documentation page

+0

文檔鏈接到期,如果可能的話,請更新到最新的鏈接。 –

2

你可以把下面的代碼到doPost()方法之前發送消息:

import com.google.android.gcm.server.*; 

Sender sender = new Sender(myApiKey); 
Message message = new Message.Builder().build(); 
MulticastResult result = sender.send(message, devices, 5); 
+0

您可以分享一下Result和MulticastResult的區別嗎?示例編碼如下,Result res = sender.send(message,regId,5); String id = res.getMessageId(); String cregId = res.getCanonicalRegistrationId(); String errorCode = res.getErrorCodeName(); –