FIREBASE REST连接一次,多次写入不会更改firebase中的数据

时间:2013-06-02 18:14:10

标签: firebase


我是firebase的新手,我想在我的项目中添加firebase。我写了一段java代码来测试与我的项目相关的功能。它是关于REST放置数据。 我的问题是,如果我每次放入数据时都创建新连接,firebase中的数据将会更改并触发绑定功能。如果我只创建一次连接,和 使用此连接将数据写入firebase,绑定的功能将仅触发一次(连接打开后的第一个数据)。事情就在那里 要放置很多数据,如果我每次都新建/打开连接,它会降低性能。有办法解决这个问题吗?或者也许这不是问题,而是我所做的  不对。任何建议将不胜感激。谢谢。 这是代码:

    String httpsURL = "https://xxxx.firebaseio.com/users.json";
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    con.setRequestMethod("PUT");
    con.setDoOutput(true);
    con.setDoInput(true);

    DataOutputStream output = new DataOutputStream(con.getOutputStream());  

    JSONObject json = new JSONObject();
    json.put("first", "xxx");
    json.put("last", "yyy");        
    System.out.println(json.toString());
    output.writeBytes(json.toString());
    output.flush();        

    json.put("first", "aaa");
    json.put("last", "bbbb");        
    System.out.println(json.toString());
    output.writeBytes(json.toString());
    output.flush();

    json.put("first", "eee");
    json.put("last", "rrrr");        
    System.out.println(json.toString());
    output.writeBytes(json.toString());
    output.flush();
    input = new DataInputStream( con.getInputStream() );
    for( int c = input.read(); c != -1; c = input.read() )
    System.out.print( (char)c );

    System.out.println("Resp Code:"+con .getResponseCode());
    System.out.println("Resp Message:"+ con .getResponseMessage());
    input.close();
    output.close();
    con.disconnect(); 

1 个答案:

答案 0 :(得分:0)

您正在发送包含多个数据块的单个HTTP PUT请求。

您需要为要写入的每个数据发送一个HTTP请求。这并不意味着您必须重新建立TCP连接。它只是意味着您需要使用新的数据blob构造和发送新的HTTP请求。

您也可能希望为每个请求写入不同的URL(或使用POST),这样就不会覆盖现有数据。