• <i id='uwBWl'><tr id='uwBWl'><dt id='uwBWl'><q id='uwBWl'><span id='uwBWl'><b id='uwBWl'><form id='uwBWl'><ins id='uwBWl'></ins><ul id='uwBWl'></ul><sub id='uwBWl'></sub></form><legend id='uwBWl'></legend><bdo id='uwBWl'><pre id='uwBWl'><center id='uwBWl'></center></pre></bdo></b><th id='uwBWl'></th></span></q></dt></tr></i><div id='uwBWl'><tfoot id='uwBWl'></tfoot><dl id='uwBWl'><fieldset id='uwBWl'></fieldset></dl></div>
    <tfoot id='uwBWl'></tfoot>

    1. <small id='uwBWl'></small><noframes id='uwBWl'>

        • <bdo id='uwBWl'></bdo><ul id='uwBWl'></ul>

        <legend id='uwBWl'><style id='uwBWl'><dir id='uwBWl'><q id='uwBWl'></q></dir></style></legend>

        HTTP 发布请求:错误 400,Firebase 主题消息传递

        时间:2023-07-29
          <bdo id='72sbf'></bdo><ul id='72sbf'></ul>

            <small id='72sbf'></small><noframes id='72sbf'>

                <i id='72sbf'><tr id='72sbf'><dt id='72sbf'><q id='72sbf'><span id='72sbf'><b id='72sbf'><form id='72sbf'><ins id='72sbf'></ins><ul id='72sbf'></ul><sub id='72sbf'></sub></form><legend id='72sbf'></legend><bdo id='72sbf'><pre id='72sbf'><center id='72sbf'></center></pre></bdo></b><th id='72sbf'></th></span></q></dt></tr></i><div id='72sbf'><tfoot id='72sbf'></tfoot><dl id='72sbf'><fieldset id='72sbf'></fieldset></dl></div>
                <legend id='72sbf'><style id='72sbf'><dir id='72sbf'><q id='72sbf'></q></dir></style></legend>

                <tfoot id='72sbf'></tfoot>
                  <tbody id='72sbf'></tbody>
                • 本文介绍了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 主题消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <legend id='VLOoq'><style id='VLOoq'><dir id='VLOoq'><q id='VLOoq'></q></dir></style></legend>
                • <small id='VLOoq'></small><noframes id='VLOoq'>

                  • <bdo id='VLOoq'></bdo><ul id='VLOoq'></ul>
                    <tfoot id='VLOoq'></tfoot>

                      1. <i id='VLOoq'><tr id='VLOoq'><dt id='VLOoq'><q id='VLOoq'><span id='VLOoq'><b id='VLOoq'><form id='VLOoq'><ins id='VLOoq'></ins><ul id='VLOoq'></ul><sub id='VLOoq'></sub></form><legend id='VLOoq'></legend><bdo id='VLOoq'><pre id='VLOoq'><center id='VLOoq'></center></pre></bdo></b><th id='VLOoq'></th></span></q></dt></tr></i><div id='VLOoq'><tfoot id='VLOoq'></tfoot><dl id='VLOoq'><fieldset id='VLOoq'></fieldset></dl></div>
                          <tbody id='VLOoq'></tbody>