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

HTTP Post Request: Error 400, Firebase Topic Messaging(HTTP 发布请求:错误 400,Firebase 主题消息传递)

本文介绍了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 主题消息传递

基础教程推荐