Retrofit post using Firebase

后端 未结 3 457
梦如初夏
梦如初夏 2020-12-10 04:51

Please any one please help. This is My API method

@POST(\"/user/new.json\")
Call createUser(@Body User user);

This is my call in Main

3条回答
  •  忘掉有多难
    2020-12-10 05:50

    I don't think you have to use Retrofit to post data.

    There is Way to Saving data in Firebase. May you know that Firebase managing all the thing in background itself.

    I don't know why are you posting data using Retrofit.

    Snippet to send and update automatically.

    public class User {
        private int birthYear;
        private String fullName;
    
        public User() {}
    
        public User(String fullName, int birthYear) {
            this.fullName = fullName;
            this.birthYear = birthYear;
        }
    
        public long getBirthYear() {
            return birthYear;
        }
    
        public String getFullName() {
            return fullName;
        }
    }
    
    Firebase alanRef = ref.child("users").child("alanisawesome");
    
    User alan = new User("Alan Turing", 1912);
    
    alanRef.setValue(alan);
    

    You can also save data directly to a database location:

    //Referencing the child node using a .child() on it's parent node alansRef.child("fullName").setValue("Alan Turing"); alansRef.child("birthYear").setValue(1912);

    It will save as you want:

    {
      "users": {
        "alanisawesome": {
          "birthYear": "1912",
          "fullName": "Alan Turing"
        }
      }
    }
    

    Please follow the docs: Saving Data in Firebase

    Thank you.

提交回复
热议问题