0

我想向我的设备发送 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>
4

3 回答 3

2

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

https://android.googleapis.com/gcm/send
于 2012-08-14T15:03:05.613 回答
1

I was getting the same error when I tried to send a message via GCM (I did it from PHP, but I don't think that really matters here).

Here is a link to my question:

Google GCM server returns 404 error

I already found out what my problem was(and answered my own question :) ). Error appeared every time I tried to pass null value to GCM. I replaced null by empty string and all is good. I don't know why, documentation doesn't say anything about this problem, but that is it.

So this may be why you are getting this error or maybe you just have bad link to the GCM server.

于 2012-09-21T19:09:28.330 回答
0

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

可能是在这些点之前发生了错误(“消息”和“CollapseKey”值是否正确传递?),并且仅在该过程的后期被捕获,为您提供您看到的堆栈跟踪。

于 2012-08-16T16:03:39.160 回答