我正打算实施GCM。我编写了一个测试代码以了解其工作原理,但在响应中不断出现错误400。
我正在用Java(JDK 7)编写。
在这个主题上,我遵循了这一原则。我在那里修改了给定的代码,并将ObjectMapper的用法更改为Gson。
这是我对数据对象的代码:
public class Data { private ArrayList<String> registration_ids; public Data() { this.registration_ids = new ArrayList<String>(); 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<Data>() {}.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 位于java.lang.java.sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)处sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)处sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native方法)在sun.net.www.protocol.http.HttpURLConnection $ 6.run(HttpURLConnection.java:1675)的.newInstance(Constructor.java:526)在sun.net.www.protocol.http.HttpURLConnection $ 6.run(HttpURLConnection.java)的。 :1673),位于sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1671)上的java.security.AccessController.doPrivileged(本机方法),位于sun.net.www.protocol.http.HttpURLConnection.getInputStream上(httpURLConnection.java:1244),位于sun.net.www.protocol.https。Http2的HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)在App.main(App.java:8)的POST2GCM.post(POST2GCM.java:64)原因:java.io.IOException:服务器返回的HTTP响应代码:URL的400 :https://android.googleapis.com/gcm/ 在sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)发送在java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)在sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)位于POST2GCM.post(POST2GCM.java:59)...还有1个
您能帮我解决这个问题吗?非常感谢,尤瓦尔。
问题出在DataOutputStream.writeChars()和DataOutputStream.writeUTF()中。
由于@Koh提供的链接,我将post函数中的write命令更改为:
wr.write(json.getBytes("UTF-8"));
现在,消息已成功发送!