0

我尝试从http://developer.android.com/guide/google/gcm/gs.html实现 GCM

我无法向应用程序发送消息。我将 System.setProperty... 添加到 servlet 和 http.proxyHost=proxy.company.com http.proxyPort=8080 到 catalina.properties 从此页面Google 云消息传递 GCM 通过代理

Servlet: Sender sender = new Sender(myApiKey);

                Message message = new Message.Builder()
                .collapseKey("1")
                .timeToLive(3)
                .delayWhileIdle(true)
                .addData("message", "message is created at builder")
                .build();

                System.setProperty("http.proxyHost", "10.200.125.200");  
                System.setProperty("http.proxyPort", "8080"); 

                Result result = sender.send(message, reGid, 5);
                System.out.println("result of message is "+ result);

GCMIntentService.java:

@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.i(TAG, "new message= ");
Toast.makeText(this, arg1.getStringExtra("message"), Toast.LENGTH_SHORT).show();
}

sender.send 不起作用,我认为代理会阻止它。安慰:

 A?u 09, 2012 10:55:44 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE:  Servlet.service() for servlet [servCloud] in context with path [/servletCloud] threw   exception
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at     java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at    java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:612)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:270)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:327)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:199)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:950)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1090)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.google.android.gcm.server.Sender.post(Sender.java:476)
at com.google.android.gcm.server.Sender.post(Sender.java:455)
at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:170)
at com.google.android.gcm.server.Sender.send(Sender.java:121)
at servCloud.doGet(servCloud.java:85)
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(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

我如何设置 sender.send 或通过代理?

4

2 回答 2

2

GCM 端点服务器使用 HTTPS。您可以尝试以下系统属性:

System.setProperty("https.proxyHost", "10.200.125.200");  
System.setProperty("https.proxyPort", "8080");
于 2012-08-20T14:17:44.290 回答
1

我遇到了同样的问题,这里提到的 System.setProperty 没有帮助。

我在下面创建了类并重构了代码以使用 ProxySender 而不是 Sender 类,它最终可以工作。

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

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

public class ProxySender extends Sender {

    public ProxySender(String key) {
        super(key);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected HttpURLConnection getConnection(String url) throws IOException {

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("1.2.3.4.", 8080));

        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(proxy);

        return conn;

    }
}

记得修改代理服务器和端口。

于 2014-01-19T09:54:49.310 回答