用于android的GCM服务显示java.net.UnknownHostException:android.googleapis.com

用于android的GCM服务显示java.net.UnknownHostException:android.googleapis.com,第1张

概述GCMService的代码:packagecom.avilyne.gcm;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjava

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.因此无法添加对Internet的许可.
>如果我想更改主机(androID.GoogleAPIs.com),那我该怎么办呢. (好像坏了).

解决方法:

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

If your organization has a firewall that restricts the traffic to or
from the Internet, you need to configure it to allow connectivity with
GCM. The ports to open are: 5228, 5229, and 5230. GCM typically only
uses 5228, but it sometimes uses 5229 and 5230. GCM doesn’t provIDe
specific IPs, so you should allow your server to accept incoming
connections from all IP addresses contained in the IP blocks Listed in
Google’s ASN of 15169.

BTW GCM消息请求是一个简单的http帖子,没有Message Builder你必须以Json格式发布消息.

Android developer site
More information about message fields

总结

以上是内存溢出为你收集整理的用于android的GCM服务显示java.net.UnknownHostException:android.googleapis.com全部内容,希望文章能够帮你解决用于android的GCM服务显示java.net.UnknownHostException:android.googleapis.com所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1116987.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存