android的GCM服务显示java.net.UnknownHostException: android.google



GCMService代码:

package com.avilyne.gcm;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
/**
 * Servlet implementation class GCMBroadcast
 */
@WebServlet("/GCMBroadcast")
public class GCMBroadcast extends HttpServlet {
    private static final long serialVersionUID = 1L;
    // The SENDER_ID here is the "Browser Key" that was generated when I
    // created the API keys for my Google APIs project.
    private static final String SENDER_ID = "AIzaSyCOLAYwS2P3ELqnTiPs3VPHGquQy1UoEIQ";
    // This is a *cheat*  It is a hard-coded registration ID from an Android device
    // that registered itself with GCM using the same project id shown above.
    private static final String ANDROID_DEVICE = "APA91bEF-_Y7t3Vc59OGuK9gnBWDegE4g2KyVgNeVIZbjGWe-4b9FMHrL82oOEYRPVz7_GaCOHbq3PatsuU_pk8jhvGng3Xp-CAv48iPqamer8Y2aajyTvUho9hsy39uNudA8XI4ML09eUsPNH87zcuGc_v2uJj65g";
    // This array will hold all the registration ids used to broadcast a message.
    // for this demo, it will only have the ANDROID_DEVICE id that was captured 
    // when we ran the Android client app through Eclipse.
    private List<String> androidTargets = new ArrayList<String>();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GCMBroadcast() {
        super();
        // we'll only add the hard-coded *cheat* target device registration id 
        // for this demo.
        androidTargets.add(ANDROID_DEVICE);
    }
    // This doPost() method is called from the form in our index.jsp file.
    // It will broadcast the passed "Message" value.
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // We'll collect the "CollapseKey" and "Message" values from our JSP page
        String collapseKey = "";
        String userMessage = "";
        try {
            userMessage = request.getParameter("Message");
            collapseKey = request.getParameter("CollapseKey");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        // Instance of com.android.gcm.server.Sender, that does the
        // transmission of a Message to the Google Cloud Messaging service.
        Sender sender = new Sender(SENDER_ID);
        // This Message object will hold the data that is being transmitted
        // to the Android client devices.  For this demo, it is a simple text
        // string, but could certainly be a JSON object.
        Message message = new Message.Builder()
        // If multiple messages are sent using the same .collapseKey()
        // the android target device, if it was offline during earlier message
        // transmissions, will only receive the latest message for that key when
        // it goes back on-line.
        .collapseKey(collapseKey)
        .timeToLive(30)
        .delayWhileIdle(true)
        .addData("message", userMessage)
        .build();
        try {
            // use this for multicast messages.  The second parameter
            // of sender.send() will need to be an array of register ids.
            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);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // We'll pass the CollapseKey and Message values back to index.jsp, only so
        // we can display it in our form again.
        request.setAttribute("CollapseKey", collapseKey);
        request.setAttribute("Message", userMessage);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
}
    因为它是一个web服务,它没有android_manifest.xml。
  1. 如果我想改变主机(android.googleapis.com),那么我应该怎么做。(似乎坏了)。

您试过关闭防火墙吗?你必须启用gcm端口。

如果您的组织有防火墙,限制流量到或您需要将其配置为允许与Internet连接GCM。开放的端口分别为:5228、5229和5230。一般只有GCM使用5228,但有时也使用5229和5230。GCM不提供特定的ip,所以您应该允许您的服务器接受传入的ip中列出的IP块中包含的所有IP地址的连接谷歌的ASN为15169.

BTW GCM消息请求是一个简单的http post,没有message Builder你必须以json格式发布消息。

Android开发者网站关于消息字段的更多信息

相关内容

最新更新