I'm trying to send some JSON notifications to the GCM servers and I'm getting this list of errors
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
Weird thing is, this only happens if I try to send to a single device. it works if I try to send to multiple devices, using the registration_id
field. The reference says error 400 means the JSON couldn't be parsed, but I tried printing it out and it seems fine:
Multicast sending: (response code: 200, invalid registration)
{"registration_ids":["asdlfiwuhfs"]}
Sending to single device: (responce code: 400, errors above)
{"to":"asdlfiwuhfs"}
The functions to send to a single or multiple devices have only one different line. One of them adds the to
field, the other a registration_id
field.
obj.put("registration_ids", regIds); // multiple devices
obj.put("to", regId); // single device
What could be the problem?
Edit:
The third line below is line 181 mentioned in the errors:
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); // 181
I found the solution looking at this code on github.
It seems getInputStream()
is only used when the response code is 200
. Otherwise, getErrorStream()
should be used.
So changing my code to this fixed it.
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()));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.