2015-05-20 143 views
1

我正在實施GCM。 我寫了一個測試代碼來了解它是如何工作的,但是我一直在響應中收到錯誤400。服務器端GCM返回錯誤400

我正在寫Java(JDK 7)。

我跟着this toturial在這個問題上。 我在那裏修改了給定的代碼,並將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. 
    } 
} 

*我在server reference看到,在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 在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本機方法) 在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在java.lang.reflect.Constructor.newInstance(Constructor.java:526) at sun.net.www.protocol.http.HttpURLConnection $ 6.run(Htt pURLConnection.java:1675) at sun.net.www.protocol.http.HttpURLConnection $ 6.run(HttpURLConnection.java:1673) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www。 protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1671) 在sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1244) 在sun.net.www.protocol.https.HttpsURLConnectionImpl。 getInputStream(HttpsURLConnectionImpl.java:254) at POST2GCM.post(POST2GCM.java:64) at App.main(App.java:8) 導致:java.io.IOException:服務器返回的HTTP響應代碼:400 for URL:https://android.googleapis.com/gcm/send at 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更多

你能幫我解決這個問題嗎? 非常感謝, Yuval。

+1

您可以使用發件人和消息幫助程序類。他們將完成所有的JSON翻譯併發送給您,這樣您就不需要自己處理輸出流。 (見https://github.com/google/gcm/blob/master/samples/gcm-demo-appengine/src/com/google/android/gcm/demo/server/SendMessageServlet.java#) L121-L124)。 – Koh

回答

1

問題出在DataOutputStream.writeChars()和DataOutputStream.writeUTF()。

多虧了@Koh給出的鏈接,我在後功能改變了寫命令:

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

現在被成功發送的消息!

相關問題