使用Firebase改造后 [英] Retrofit post using Firebase

查看:131
本文介绍了使用Firebase改造后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请任何人请帮忙。这是我的API方法


$ b $ @ $($ / $ / $ new $)
调用createUser(@Body用户用户);

这是我在 MainActivity p>

  Retrofit retrofit = new Retrofit.Builder()。baseUrl(https://XXXXXX.firebaseio.com).addConverterFactory(GsonConverterFactory 。.create())建立(); 

Api api = retrofit.create(Api.class);

用户用户=新用户(1,Sam);

致电<使用者>调用= api.createUser(用户);
call.enqueue(new Callback< User>(){
@Override
public void onResponse(Call< User> call,Response< User> response){
Log.d (sam,run);
}

@Override
public void onFailure(Call< User> call,Throwable t){
Log.d (sam,error);
}
});

这是 User.java

  public class User {

int id;

字符串名称;
$ b $ public User(int id,String name){
this.id = id;
this.name = name;


$ / code $ / pre
$ b $输出即将如此: -


$ b $

 user:{new:{-KBgcQTomo8xGpnv5raM:{id:1,name:Sam}但是我想要的输出是这样的: -  $ / 







 user:{new:{id:1,name:Sam}} 

以下是 Retrofit + Firebase



请帮忙...............

POST 时正尝试推送到存储在POST网址下的数据列表 。因此,POST到/user/new.json是不可能的,并且不会将数据存储在新的firebase生成密钥(如/ user / new下面的-KBgcQTomo8xGpnv5raM)下。

如果您想完全控制数据的放置位置,您必须使用 PUT 。但是,直接把你的新条目作为/ user / new是没有意义的。如果你不接受服务器端的密钥分配,那么正常的解决方案是使用你将强制唯一性的一部分条目。例如,名称或数字ID可能是新用户的关键,以便可以添加多个用户。



基于改进API并使用name作为这个:

$ pre $ @ $($ / $ / $ / $ new $)
调用createUser(@Body用户用户);

会变成:

  @PUT(/ user / new / {name} .json)
调用createUser(@Path(name)字符串名称,@Body User user);



 通话<使用者>调用= api.createUser(用户); 

会是:

 通话<使用者> call = api.createUser(user.name,user); 

现在的布局是:

 user:{new:{Sam:{id:1,name:Sam}}} 

所以未来的用户可以被添加,只要他们没有被命名为Sam。


Please any one please help. This is My API method

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

This is my call in MainActivity

 Retrofit retrofit=new Retrofit.Builder().baseUrl("https://XXXXXX.firebaseio.com").addConverterFactory(GsonConverterFactory.create()).build();

    Api api=retrofit.create(Api.class);

    User user=new User(1,"Sam");

    Call<User> call=api.createUser(user);
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            Log.d("sam","run");
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Log.d("sam","error");
        }
    });

This is User.java

public class User {

        int id;

        String name;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

Output is coming like that :-

"user" : {"new" : {"-KBgcQTomo8xGpnv5raM" : {"id" : 1,"name" : "Sam"}}}

But i want output like that :-

"user" : {"new" : {"id" : 1,"name" : "Sam"}}

Here is Tutorial For Retrofit + Firebase

please help................

解决方案

In firebase, when you POST you are attempting to push to a list of data stored under the POST's URL. Consequently, it is impossible to POST to /user/new.json and not store the data under a new firebase generated key like "-KBgcQTomo8xGpnv5raM" beneath /user/new.

If you want complete control of where you are putting the data you must use PUT. However, putting your new entry directly as /user/new would not make sense. Where will subsequent entries go?

If you are not accepting server side keys assignment, then the normal solution would be to use some part of the entry that you will enforce uniqueness on. For example, either the name or numeric id could be the key for the new user so that multiple users may be added.

Based on the retrofit API and using name as the unique key, this:

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

would become:

@PUT("/user/new/{name}.json")
Call createUser(@Path("name") String name, @Body User user);

and:

Call<User> call=api.createUser(user);

would then be:

Call<User> call=api.createUser(user.name, user);

now the layout would be:

"user" : {"new" : {"Sam": {"id" : 1,"name" : "Sam"}}}

so future users could be added as long as they were not named Sam.

这篇关于使用Firebase改造后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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