2012-12-20 104 views
0

我試圖向Android設備發送推送通知,但在以下情況下出現錯誤。使用GCM時發生IOException

String msg = "push message"; 
String to1 = "123456"; 
String from1 = "098776655"; 

這工作得很好:

Message message = new Message.Builder() 
    .collapseKey("1") 
    .addData("message", msg) 
    .addData("to1", to1) 
    .addData("to2", to1) 
    .build(); 

但使用以下時收到錯誤:

Message message = new Message.Builder() 
    .collapseKey("1") 
    .addData("message", msg) 
    .addData("to", to1) 
    .addData("from", from1) 
    .build(); 

我甚至代替from1使用的其他變量爲好,但仍然得到錯誤:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send 

堆棧跟蹤:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send 
    [run]  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    [run]  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    [run]  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    [run]  at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    [run]  at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1298) 
    [run]  at java.security.AccessController.doPrivileged(Native Method) 
    [run]  at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1292) 
    [run]  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:948) 
    [run]  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234) 
    [run]  at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:363) 
    [run]  at com.google.android.gcm.server.Sender.send(Sender.java:261) 
    [run]  at example.HelloServlet.sendToSingleDevice(HelloServlet.java:173) 
    [run]  at example.HelloServlet.doGet(HelloServlet.java:36) 
    [run]  at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    [run]  at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    [run]  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    [run]  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    [run]  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    [run]  at java.lang.reflect.Method.invoke(Method.java:597) 
    [run]  at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:270) 
    [run]  at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269) 
    [run]  at java.security.AccessController.doPrivileged(Native Method) 
    [run]  at javax.security.auth.Subject.doAsPrivileged(Subject.java:517) 
    [run]  at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:302) 
    [run]  at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:163) 
    [run]  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:283) 
    [run]  at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56) 
    [run]  at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189) 
    [run]  at java.security.AccessController.doPrivileged(Native Method) 
    [run]  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185) 
    [run]  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    [run]  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    [run]  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    [run]  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    [run]  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    [run]  at net.stax.appserver.webapp.RequestMonitorValve.invoke(RequestMonitorValve.java:35) 
    [run]  at net.stax.appserver.webapp.RequestSetupValve.invoke(RequestSetupValve.java:31) 
    [run]  at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:647) 
    [run]  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    [run]  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    [run]  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) 
    [run]  at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    [run]  at java.lang.Thread.run(Thread.java:619) 
    [run] Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send 
    [run]  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245) 
    [run]  at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373) 
    [run]  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318) 
    [run]  at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:362) 
    [run]  ... 33 more 
    [run] Exception Message : Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send 
+1

爲什麼你甚至會嘗試發送兩個具有相同名稱但不同值的參數? –

+0

對不起,這只是一個輸入錯誤在這裏,我現在已經糾正... – Arsalan

+0

根據_GCM_文檔,確切的失敗原因在響應中描述。 –

回答

0

謝謝,每次身體我有固定它...

的問題是什麼,我只是改變

.addData("from", from1) 

.addData("from1", from1) 

和它的作品...

可能是from是GCM服務器端的關鍵字...

0

這可能是您沒有權限訪問該網址,或者您需要安裝新的證書,由服務器提供的證書,你需要跟服務器端的接觸!

+0

正如我上面提到的那樣,它在第一種情況下工作,但不在第二種情況下工作...... – Arsalan

0

如果你看看你從服務器(響應體)得到的響應,它會告訴你,from確實是一個保留字(因爲它在Android中用於Intents,據我所知) 。

相關問題