gpt4 book ai didi

java - 服务器端 GCM 返回错误 400

转载 作者:行者123 更新时间:2023-11-30 02:09:10 25 4
gpt4 key购买 nike

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

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

我关注了this关于这个主题的教程。我在那里修改了给定的代码,并将 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 协议(protocol)中,我可以仅使用 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: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.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) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1244) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at POST2GCM.post(POST2GCM.java:64) at App.main(App.java:8) Caused by: java.io.IOException: Server returned HTTP response code: 400 for 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) at POST2GCM.post(POST2GCM.java:59) ... 1 more

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

最佳答案

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

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

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

现在消息发送成功!

关于java - 服务器端 GCM 返回错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com