服务器返回HTTP响应代码:400为URL:https://fcm.googleapis.com/fcm/send

时间:2018-03-12 09:47:36

标签: firebase firebase-cloud-messaging

代码

    deviceIds = mbln.getGCMId();  //getting arraylist
    String[] s = new String[deviceIds.size()];
    for (int i =0; i < deviceIds.size(); i++)
        s[i] = deviceIds.get(i);     //converting to array
    logger.debug(s);
    JSONObject info = new JSONObject();

    info.put("registration_ids", s); //device registration token
    info.put("title", "HI1");
    info.put("body", "hello");  

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(info.toString());
    wr.flush();
    wr.close();

    int responseCode = conn.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);   //response = 400
        }

错误

java.io.IOException:服务器返回HTTP响应代码:400为URL:https://fcm.googleapis.com/fcm/send

它与&#34;对#34;但要向我使用的多个设备发送通知&#34; registration_ids&#34;我必须传递令牌的String数组。所以那里出了点问题。

1 个答案:

答案 0 :(得分:1)

所以答案是:

我正在使用字符串数组,而不是我只使用ArrayList存储字符串,它解决了问题。

所以而不是

String[] s = new String[deviceIds.size()];

创建一个ArrayList,

ArrayList<String> s = new ArrayList<String>();