【问题标题】:Google Firebase HTTP Post谷歌 Firebase HTTP 发布
【发布时间】:2017-06-29 23:22:34
【问题描述】:

我正在尝试为服务器中的 Google Firebase 请求 POST。我遵循了文档指南,但没有奏效。

我的发送消息功能是跟随。

private static void sendMsg() throws IOException 
{
    String url = "https://fcm.googleapis.com/fcm/send";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Authorization", "key=XXXX");

    JSONObject msg=new JSONObject();
    msg.put("message","test8");

    JSONObject parent=new JSONObject();

    parent.put("to", "XXXXX");
    parent.put("data", msg);

    con.setDoOutput(true);

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + parent.toString());
    System.out.println("Response Code : " + responseCode+" "+con.getResponseMessage());

}

响应代码是“411”,消息是“需要长度”。 我也试过设置内容长度,但结果是一样的。

我做错了吗?

【问题讨论】:

    标签: java firebase firebase-cloud-messaging


    【解决方案1】:

    您已完成所有设置,但没有写入数据。添加显示的语句:

    con.setDoOutput(true);
    // Added
    OutputStreamWriter os = new OutputStreamWriter(con.getOutputStream());
    os.write(parent.toString());
    os.flush();
    os.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 2017-07-27
      相关资源
      最近更新 更多