HTTP Post Request:错误400,Firebase主题消息 [英] HTTP Post Request: Error 400, Firebase Topic Messaging
问题描述
我正在尝试在Android应用程序中实现Firebase主题消息,我正在尝试构建HTTP post请求,并且我收到的响应代码为400.我已经查看了各种解决方案,但没有一个似乎有所帮助。
I'm trying to implement Firebase Topic Messaging in an Android application, and I'm attempting to build a HTTP post request, and I'm receiving a response code of 400. I have looked at various solutions but none of them have seemed to help.
这里是我调用AsyncTask的子类的地方:
Here is where I call the subclass of AsyncTask:
try{new FirebaseSendMessage().execute("Hello world");}
catch (Exception e) {
Log.d("Exception", e.toString());
}
这是我的Async Task类的子类。
Here is my Async Task class's subclass.
class FirebaseSendMessage extends AsyncTask<String, Integer, Double> {
private final static String USER_AGENT = "Mozilla/5.0";
private final static String AUTH_KEY = "<My firebase authorization key obtained from firebase>";
private Exception exception;
protected Double doInBackground(String... params) {
try {
sendRequest(params);
} catch (Exception e) {
this.exception = e;
}
return null;
}
protected void onPostExecute(Long l) {
// TODO: check this.exception
// TODO: do something with the feed
}
public void sendRequest(String... params) {
try {
String urlString = "https://fcm.googleapis.com/fcm/send";
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "key=" + AUTH_KEY);
String postJsonData = "{\"to\": \"/topics/news\"\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postJsonData);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK){
System.out.println("succeeded");
}
/*InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//con.disconnect();*/
}
catch(IOException e){
Log.d("exception thrown: ", e.toString());
}
}
}
错误: I / System.out:POST响应代码:: 400
请让我知道是否需要其他代码片段来帮助我调试。提前致谢!
Please let me know if there are additional code snippets required to help me debug. Thanks in advance!
推荐答案
错误400 表示您的请求中包含无效的JSON:
Error 400 means an Invalid JSON in your request:
检查JSON消息是否格式正确并包含有效字段(例如,确保传入正确的数据类型)。
Check that the JSON message is properly formatted and contains valid fields (for instance, making sure the right data type is passed in).
在 sendRequest
中,您错过了news之间的逗号(
和,
) \\ \data \
和结束括号(}
) :
In your sendRequest
, you missed a comma (,
) between "news\"
and \"data\"
and a closing bracket (}
):
String postJsonData = "{\"to\": \"/topics/news\"\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}";
如下所示:
{"to": "/topics/news/""data":{"message":"...."}
应该是:
String postJsonData = "{\"to\": \"/topics/news\", \"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}}";
因此JSON结构是正确的:
So that the JSON structure would be correct:
{"to": "/topics/news/",
"data":{"message":"..."}
}
这篇关于HTTP Post Request:错误400,Firebase主题消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!