HTTP Post Request: Error 400, Firebase Topic Messaging(HTTP 发布请求:错误 400,Firebase 主题消息传递)
问题描述
我正在尝试在 Android 应用程序中实现 Firebase 主题消息传递,并且我正在尝试构建 HTTP 发布请求,我收到的响应代码为 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());
}
这是我的异步任务类的子类.
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"<之间的逗号 (
,
)/code> 和右括号 (}
):
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 发布请求:错误 400,Firebase 主题消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HTTP 发布请求:错误 400,Firebase 主题消息传递
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01