问题描述
我正在努力实施GCM。我写了一个测试代码来了解它是如何工作的,但是我在响应中一直收到错误400.
我正在用Java编写(JDK 7) 。
我遵循 toturial的主题。
我在那里修改了给定的代码,并将ObjectMapper的使用改为Gson。
这是我的Data对象的代码:
public class Data {
private ArrayList< String> registration_ids;
public Data(){
this.registration_ids = new ArrayList< String>();
this.registration_ids.add(APA91 ...); //这里有真实的设备注册ID。
}
}
*我在,在HTTP协议中,我可以使用registration_ids发送消息。 (所有其他都是可选的)
以下是发送消息的代码:
b
$ b // public static void post(String apiKey,Data data){
// // 1. URL
URL url = new URL( https://android.googleapis.com/gcm/send);
// 2.打开连接
HttpURLConnection conn =(HttpURLConnection)url.openConnection();
// 3.指定POST方法
conn.setRequestMethod(POST);
// 4.设置头文件
conn.setRequestProperty(Content-Type,application / json);
conn.setRequestProperty(Authorization,key =+ apiKey);
conn.setDoOutput(true);
// 5.将JSON数据添加到POST请求体中
// 5.1将对象转换为JSON
Gson gson = new Gson();
Type type = new TypeToken< Data>(){} .getType();
字符串json = gson.toJson(data,type);
System.out.println(json);
//打印的字符串是
// {registration_ids:[APA91 ...]}
//带有我的设备的正确注册ID。
// 5.2获取连接输出流
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
// 5.3将内容JSON复制到
wr.writeUTF(json);
// 5.4发送请求
wr.flush();
// 5.5关闭
wr.close();
//获取响应
int responseCode = conn.getResponseCode();
System.out.println(\\\
发送'POST'请求到URL:+ url);
System.out.println(Response Code:+ responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer(); ((inputLine = in.readLine())!= null){
response.append(inputLine);
}
in.close();
// 7.打印结果
System.out.println(response.toString());
catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
我知道错误400意味着json问题,但我为我发送的数据尝试了很多变化,并且它们都没有工作(我在这些测试中手动创建了字符串,没有gson)。
这是错误消息我收到:
你能帮我解决这个问题吗?
非常感谢,
Yuval。
问题出在DataOutputStream.writeChars()和DataOutputStream.writeUTF()。
感谢,我将post函数中的write命令更改为:
wr.write(json.getBytes(UTF- 8\" ));
现在邮件已成功发送!
I am tring to implement GCM.I wrote a test code to understand how it works, but I keep getting error 400 in the response.
I'm writing in Java (JDK 7).
I followed this toturial on the subject.I modified the given code there, and changed the use of ObjectMapper to Gson.
This is my code for the Data object:
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.
}
}
*I saw in the server reference that in HTTP protocol, I can send a message just with registration_ids. (All other are optional)
Here is the code to send the message:
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();
}
}
I know that error 400 means a json problem, but I tried many variations for the data I'm sending, and none of them worked (I created the string manually, without gson, in these tests).
This is the error message I'm receiving:
Can you please help me with this issue?Thank you very much,Yuval.
The problem was in DataOutputStream.writeChars() and DataOutputStream.writeUTF().
Thanks to the link given by @Koh, I changed the write command in the post function to:
wr.write(json.getBytes("UTF-8"));
Now the message is being sent successfully!
这篇关于服务器端GCM返回错误400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!