Firestore 使用 POST 请求和 HttpsURLConnection 将字段添加到文档

Firestore add Field to Document with POST request and HttpsURLConnection

即使在使用 Google 的 APIs Explorer 玩了好几个小时后,我还是无法弄明白。我想使用提供文档路径的 HTTPS 请求添加单个字段。

我的 PATCH 函数示例(这个有效)

private void PATCHRequest(String document_path, String theData) throws IOException, JSONException {

    // Build URL
    String FirestoreURL = REST_HEADER + "projects/" + PROJECT_ID + "/databases/(default)/documents/" + document_path;

    // Create URL
    URL cloudFirestoreEndPoint = new URL(FirestoreURL);

    // Create connection
    myHttpsConnection = (HttpsURLConnection) cloudFirestoreEndPoint.openConnection();

    // Set Writable
    myHttpsConnection.setDoOutput(true);

    // Set Request Method
    myHttpsConnection.setRequestMethod("PATCH");

    // Set Https Connection properties
    myHttpsConnection.setRequestProperty("Content-Type", "application/json");

    // Generate JSON from the data
    JSONObject myJSON = getJSONfromString(theData);

    if(myJSON != null){

        // Create output stream linked to our https connection
        OutputStreamWriter streamWriter = new OutputStreamWriter(myHttpsConnection.getOutputStream());

        // Write to buffer
        streamWriter.write(myJSON.toString());

        // Send out the buffer
        streamWriter.flush();

        // Close the stream
        streamWriter.close();
    }
}

其他示例我的 DELETE 函数,

private void DELETERequest(String document_path) throws IOException {

    // Build URL
    String FirestoreURL = REST_HEADER + "projects/" + PROJECT_ID + "/databases/(default)/documents/" + document_path;

    // Create URL
    URL cloudFirestoreEndPoint = new URL(FirestoreURL);

    // Create connection
    myHttpsConnection = (HttpsURLConnection) cloudFirestoreEndPoint.openConnection();

    // Set Writable
    myHttpsConnection.setDoOutput(true);

    // Set Request Method
    myHttpsConnection.setRequestMethod("DELETE");

    // Send the command
    myHttpsConnection.connect();

    // Result
    myResult_ = "deleted";
}

基于这种格式,有人知道如何添加带有值的单个字段吗?

我需要使用 updateMask 字段,

        if(fieldMasks_ != null) {
            for (int i = 0; i < fieldMasks_.size(); ++i) {
                URL_str.append("updateMask.fieldPaths=").append(fieldMasks_.get(i).getName()).append("&");
            }
        }