Java 发送GCM通知时发生IOException

Java 发送GCM通知时发生IOException,java,android,json,google-cloud-messaging,Java,Android,Json,Google Cloud Messaging,我正在尝试向GCM服务器发送一些JSON通知,我得到了这个错误列表 Response Code: 400 java.io.IOException: Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

我正在尝试向GCM服务器发送一些JSON通知,我得到了这个错误列表

Response Code: 400
java.io.IOException: Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
        at controller.Notification.post(Notification.java:181)
        at controller.App.main(App.java:17)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        at java.net.HttpURLConnection.getResponseCode(Unknown Source)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
        at controller.Notification.post(Notification.java:179)
        ... 1 more
奇怪的是,只有当我尝试发送到单个设备时才会发生这种情况。如果我尝试使用
registration\u id
字段发送到多个设备,它会起作用。参考称错误400意味着无法解析JSON,但我尝试打印出来,结果似乎很好:

多播发送:(响应代码:200,注册无效)

发送到单个设备:(响应代码:400,以上错误)

要发送到单个或多个设备的函数只有一条不同的行。其中一个将
添加到
字段,另一个将
注册id添加到
字段

obj.put("registration_ids", regIds); // multiple devices
obj.put("to", regId);                // single device
有什么问题吗

编辑:

下面第三行是错误中提到的第181行:

int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); // 181

我在github上找到了解决方案

似乎只有在响应代码为
200
时才使用
getInputStream()。否则,应使用
getErrorStream()

所以把我的代码改成这个就解决了

int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);

BufferedReader in = null;
if (responseCode == 200)
    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
else
    in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));

发送到多个设备时,url的形式是什么?请把它贴在这里
400是错误的请求,并不是说JSON无法解析。
顺便说一句……来自:仅适用于JSON请求。指示无法将请求解析为JSON,或者该请求包含无效字段(例如,在需要数字的位置传递字符串)。响应中描述了确切的失败原因,应该在重试请求之前解决该问题。您是否可以添加断点并检查响应的内容?或者抓住做这件事的例外?
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); // 181
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);

BufferedReader in = null;
if (responseCode == 200)
    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
else
    in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));