使用Google App Engine(GAE)无法连接到Google Cloud Messaging(GCM)服务器 [英] Can't connect to Google Cloud Messaging (GCM) server using Google App Engine (GAE)

查看:98
本文介绍了使用Google App Engine(GAE)无法连接到Google Cloud Messaging(GCM)服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用设置Google云消息传递,并且正在为我的服务器使用Google App Engine。我有我的API密钥,但我似乎无法连接到谷歌云消息服务器。这是我的代码。

I am trying to set up google cloud messaging for my app, and I am using Google App Engine for my server. I have my API key but I can't seem to make a connection to the google cloud messaging servers. Here is my code.

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://android.googleapis.com/gcm/send");
        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("registration_id", regId));
            nameValuePairs.add(new BasicNameValuePair("data.message", messageText));    

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            post.setHeader("Authorization", "key=*MY_API_KEY_HERE*");
            post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");


            Header[] s=post.getAllHeaders();


            System.out.println("The header from the httpclient:");

            for(int i=0; i < s.length; i++){
            Header hd = s[i];

            System.out.println("Header Name: "+hd.getName()
                    +"       "+" Header Value: "+ hd.getValue());
            }


            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
            System.out.println(line);
            }

            } catch (IOException e) {
                e.printStackTrace();
        }

当我查看日志时,标题的设置是正确的。然而,我得到一个错误,说:

When I look at the log the headers are being setup right. However, I get an error that says


org.apache.http.impl.client.DefaultRequestDirector tryConnect:I / O异常(java。 net.SocketException)在连接到目标主机时被捕获:权限被拒绝:尝试未经许可访问被阻止的收件人。 (mapped-IPv4)

org.apache.http.impl.client.DefaultRequestDirector tryConnect: I/O exception (java.net.SocketException) caught when connecting to the target host: Permission denied: Attempt to access a blocked recipient without permission. (mapped-IPv4)

我在Google API控制台上关闭了Google云消息传递功能,并检查了我的API密钥a一堆。我不知道为什么我会被拒绝。在战争中我需要一个罐子还是我必须放在清单中?

I've turned google cloud messaging on in the Google APIs Console and I've checked my API key a bunch of times. I have no clue why I'm getting rejected. Is there a jar I need in the war or something I have to put in the manifest?

感谢您阅读本文!
Mark

Thanks for reading this!! Mark

推荐答案

我遇到了同样的问题,我正在使用类似于您正在使用的内容。

I had the same problem and I was using something similar to what you are using.


  1. 我必须在我的GAE应用程序上启用结算功能(您可能有这个功能,但我不知道我必须) li>
  2. 阅读https://developers.google.com/appengine/docs/java/sockets/和https://developers.google.com/appengine/docs/java/urlfetch/

因此,我之前看起来像您的代码看起来如下所示:

Therefore my code which looked like yours before now looks like following:

String json ="{}"; 
URL url = new URL("https://android.googleapis.com/gcm/send");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.addHeader(new HTTPHeader("Content-Type","application/json")); 
request.addHeader(new HTTPHeader("Authorization", "key=<>"));
request.setPayload(json.getBytes("UTF-8"));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);

这篇关于使用Google App Engine(GAE)无法连接到Google Cloud Messaging(GCM)服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆