HTTP Post Request:错误400,Firebase主题消息

时间:2017-01-10 02:41:04

标签: android http firebase http-post firebase-cloud-messaging

我尝试在Android应用程序中实施Firebase主题消息,并且我尝试构建HTTP发布请求,并且我收到的响应代码为400.我查看了各种解决方案,但没有一个似乎有所帮助。

这是我调用AsyncTask的子类的地方:

try{new FirebaseSendMessage().execute("Hello world");}
                catch (Exception e) {
                    Log.d("Exception", e.toString());
                }

这是我的Async Task类的子类。

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 Response Code :: 400

如果有其他代码段可以帮助我调试,请告诉我。提前谢谢!

2 个答案:

答案 0 :(得分:6)

Error 400表示您的请求中包含无效的JSON:

  

检查JSON消息是否格式正确并包含有效字段(例如,确保传入正确的数据类型)。

sendRequest中,您错过了,"news\"之间的逗号(\"data\")和结束括号(}):

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结构就是正确的:

{"to": "/topics/news/",
 "data":{"message":"..."}
}

答案 1 :(得分:-1)

对于那些愿意在您的应用程序中使用身份验证密钥的人。

我建议您通过应用程序的SHA-1手动加密密钥,并在运行时使用SHA-1代码对其进行解密。