Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

android - HTTP Post Request: Error 400, Firebase Topic Messaging

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.

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());
    }
}

}

Error: I/System.out: POST Response Code :: 400

Please let me know if there are additional code snippets required to help me debug. Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...