服务器端GCM返回错误400

我想实施GCM。 我写了一个测试代码来了解它是如何工作的,但我在响应中不断收到错误400。

我正在用Java编写(JDK 7)。

我跟着这个关于这个主题的动词。 我在那里修改了给定的代码,并将ObjectMapper的使用改为Gson。

这是我的Data对象代码:

public class Data { private ArrayList registration_ids; public Data() { this.registration_ids = new ArrayList(); this.registration_ids.add("APA91..."); // There is the real device registration id here. } } 

*我在服务器参考中看到,在HTTP协议中,我只能使用registration_ids发送消息。 (所有其他都是可选的)

以下是发送消息的代码:

 public static void post(String apiKey, Data data){ try{ // 1. URL URL url = new URL("https://android.googleapis.com/gcm/send"); // 2. Open connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 3. Specify POST method conn.setRequestMethod("POST"); // 4. Set the headers conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "key=" + apiKey); conn.setDoOutput(true); // 5. Add JSON data into POST request body // 5.1 Convert object to JSON Gson gson = new Gson(); Type type = new TypeToken() {}.getType(); String json = gson.toJson(data, type); System.out.println(json); // The printed string is // {"registration_ids":["APA91..."]} // with the correct registration id of my device. // 5.2 Get connection output stream DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); // 5.3 Copy Content "JSON" into wr.writeUTF(json); // 5.4 Send the request wr.flush(); // 5.5 close wr.close(); // 6. Get the response int responseCode = conn.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 7. Print result System.out.println(response.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 

我知道错误400意味着一个json问题,但我为我发送的数据尝试了很多变化,并且没有一个工作(我在这些测试中手动创建了字符串,没有gson)。

这是我收到的错误消息:

java.io.IOException:服务器返回HTTP响应代码:400为URL: https ://android.googleapis.com/gcm/send at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(在Sun.net.www.protocol.http.HttpURLConnection的java.lang.reflect.Constructor.newInstance(Constructor.java:526)的sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)中的NativeConstructorAccessorImpl.java:57) $ 6.run(HttpURLConnection.java:1675)at sun.net.www.protocol.http.HttpURLConnection $ 6.run(HttpURLConnection.java:1673)at sun.net.www.security.AccessController.doPrivileged(Native Method) .protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1671)at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1244)at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream (HttpsURLConnectionImpl.java:254)在POST2GCM.post(POST2GCM.java:64)a t App.main(App.java:8)引起:java.io.IOException:服务器返回HTTP响应代码:400为URL: https ://android.googleapis.com/gcm/send at sun.net.www。 protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)在POST2GCM.post(POST2GCM.java:59)……还有1个

你能帮我解决这个问题吗? 非常感谢,Yuval。

问题出在DataOutputStream.writeChars()和DataOutputStream.writeUTF()中。

感谢@Koh提供的链接,我将post函数中的write命令更改为:

 wr.write(json.getBytes("UTF-8")); 

现在消息正在成功发送!