FIREBASE REST连接一次,多次写入不会更改Firebase中的数据 [英] FIREBASE REST connect once and multiple write will not change data in firebase

查看:35
本文介绍了FIREBASE REST连接一次,多次写入不会更改Firebase中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是Firebase的新手,我想将Firebase添加到我的项目中.我编写了一段Java代码来测试与我的项目相关的功能.它是关于REST放置数据的.我的问题是,如果每次放置数据时都创建新的连接,firebase上的数据将更改并触发绑定的功能.如果我只创建一次连接,并且使用此连接将数据写入firebase时,绑定函数将仅触发一次(打开连接后的第一个数据).东西在那里如果要放入大量数据,并且每次我每次打开/打开连接都会降低性能.有办法解决这个问题吗?也许这不是问题,但是我做了什么是不正确的.任何建议将不胜感激.谢谢.这是代码:


I am new to firebase and I want to add firebase into my project. I wrote a piece of java code to test the feature related to my project. It is about REST put data. My issue is, if I create new connection everytime I put data, the data at firebase will change and trigger the binded function. If I only create the connection once, and write the data to firebase using this connection, the binded function will be only triggered once(the very first data immediately after the connection is open). The thing is there are a lot of data to put and it will degrade the performance if I new/open the connection everytime. Is there way to solve this problem? Or maybe it is not a problem but what I did was not correct. Any suggestion will be appreciated. Thanks. Here is the code:

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

推荐答案

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

You're sending a single HTTP PUT request with multiple chunks of data.

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

You need to send one HTTP request for every piece of data you want to write. This doesn't mean you have to re-establish a TCP connection. It just means you need to construct and send a new HTTP request with the new blob of data.

您可能还想为每个请求写一个不同的URL(或使用POST),以免覆盖现有数据.

You'll also probably want to write to a different URL for each request (or use POST) so that you don't overwrite your existing data.

这篇关于FIREBASE REST连接一次,多次写入不会更改Firebase中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆