GCM退回404错误

时间:2012-08-14 08:47:18

标签: android google-cloud-messaging

我想将natification消息发送到我的设备但是有这个错误

 com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 404

我不确定为什么。

这是我的代码

package com.avilyne.gcm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;

public class GCMBroadcast extends HttpServlet {
    private static final String myApiKey = "AIzaSyAHUdHy-7o-XJS59mhAzUUoTqn-EUb9s88";
    private static final String regID = "APA91bHUcUuv3ViHfIkZg7YyKeiq3Ta1WVqEWrdIs9AZwdQ2nfSA2r-9BcH4WxRnAb0mHXEqEi7WMngPKQyrzcP5EhCFLqsprJpscHLIyi8gK80SdMpHluLBk3iRUCQTShY8szNvy3f4IBiGZv75ioe-Ai8wnZLqKw";
    private List<String> androidTargets = new ArrayList<String>();
    private static final long serialVersionUID = 1L;

    public GCMBroadcast() {

        super();

        androidTargets.add(regID);

    }


    /*protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

            doPost( req, resp);*/


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String userMessage = "";
        userMessage = req.getParameter("Message");

        Sender sender = new Sender(myApiKey);
        Message message = new Message.Builder().addData("Message", userMessage)
                .build();

        MulticastResult result = sender.send(message, androidTargets, 1);

        if (result.getResults() != null) {
            int canonicalRegId = result.getCanonicalIds();
            if (canonicalRegId != 0) {

            }
        } else {
            int error = result.getFailure();
            System.out.println("Broadcast failure: " + error);
        }

        req.setAttribute("Message", userMessage);

        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }

}

当不使用doGet方法尝试此代码时,返回“HTTP状态405 - 此URL不支持HTTP方法GET”此错误。 İfı添加doGet方法并在doGet中调用doPost方法。它返回“HTTP状态500 - HTTP状态代码:404(未找到

未找到

错误404

)”

我尝试将消息发送到服务器时收到的完整错误消息是:

 com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 404(<HTML>
<HEAD>
<TITLE>Not Found</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Not Found</H1>
<H2>Error 404</H2>
</BODY>
</HTML>)
    at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:367)
    at com.google.android.gcm.server.Sender.send(Sender.java:261)
    at com.avilyne.gcm.GCMBroadcast.doPost(GCMBroadcast.java:49)
    at com.avilyne.gcm.GCMBroadcast.doGet(GCMBroadcast.java:35)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我不知道为什么我会收到这个错误。有人可以帮忙吗?该文档没有说明404代码。

这是web.xml代码;

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <servlet>
        <servlet-name>GCMBroadcast</servlet-name>
        <servlet-class>com.avilyne.gcm.GCMBroadcast</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>GCMBroadcast</servlet-name>
        <url-pattern>/GCMBroadcast</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

那就是index.jsp代码;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%
    // retrieve our passed CollapseKey and Message parameters, if they exist.

    String message  = "Generic Broadcast Message";

    Object msgObj =  request.getAttribute("Message");

    if (msgObj != null) {
        message = msgObj.toString();
    }

%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Android GCM Broadcast</title>

</head>
<body>

    <h2>GCM Broadcast Demo</h2>

    <form action="GCMBroadcast" method="post">
        <label>Broadcast Message </label>

        <br/><textarea name="Message" rows="3" cols="60" ><%=message %> </textarea> 
        <br/><input type="submit" name="submit" value="Submit" />
        </form>   
    </body>
</html>

3 个答案:

答案 0 :(得分:2)

发布发件人类的代码。但很可能你的GCM服务器的URL错误。它应该是一个HTTPS POST请求:

https://android.googleapis.com/gcm/send

答案 1 :(得分:1)

当我尝试通过GCM发送消息时,我遇到了同样的错误(我是用PHP做的,但我觉得这不重要。)

以下是我的问题的链接:

Google GCM server returns 404 error

我已经发现了我的问题(并回答了我自己的问题:))。 每次我尝试将空值传递给GCM时都会出现错误。我用空字符串替换了null,一切都很好。我不知道为什么,文档没有说明这个问题,但就是这样。

因此,这可能是您收到此错误的原因,或者您可能只是链接到GCM服务器的错误。

答案 2 :(得分:0)

Google提供的com.google.android.gcm.server.Sender类中嵌入了URL。最好的选择可能是通过Eclipse调试会话逐步执行doPost()以查看错误是实际发生在sender.send()时刻,还是发生在result.getResults()时刻或之前的某个地方。

可能是在这些点之前发生了错误(正确传递了“Message”和“CollapseKey”值?),并且只是稍后在进程中被捕获,为您提供了所看到的堆栈跟踪。 / p>