如何在Java中使用Firebase REST API? [英] How to use Firebase REST API in Java?

查看:261
本文介绍了如何在Java中使用Firebase REST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够将一些数据更新到firebase,而无需将firebase设置为在线,这样只有指定的数据被发送,而没有wifi可用。我一直无法找到关于如何在Java中使用REST API的文档或指南。有人可以帮我吗?

解决方案

不幸的是,现在还没有一个适用于Firebase REST界面的Java SDK )。相反,您需要使用您选择的HTTP客户端创建一个。



以下是使用Jersey的示例:

  import javax.ws.rs.core.MediaType; 
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.vnomicscorp.firebase.sdk.FirebaseSdk;
import com.vnomicscorp.firebase.sdk.FirebaseSdkException;

public class FirebaseSdkJersey {

private static final String DEFAULT_AUTH_PARAM_NAME =auth;
private static final String DEFAULT_PATH_FORMAT =%s.json;

private final String凭证;
private final String url;
私人最终客户端客户端;

private String authParamName = DEFAULT_AUTH_PARAM_NAME;
private String pathFormat = DEFAULT_PATH_FORMAT;

public FirebaseSdkJersey(String url,String credentials,Client client){
this.url = url;
this.credentials = credentials;
this.client = client;


public void setValue(String path,String value)throws Exception {
client.resource(url).path(String.format(pathFormat,path))
.queryParam(authParamName,credentials)
.type(MediaType.APPLICATION_JSON).entity(value)
.put(String.class);

$ b $ public String getValue(String path)throws Exception {
return client.resource(url).path(String.format(pathFormat,path))
.queryParam(authParamName,credentials).get(String.class);

$ b $ public void deleteValue(String path)throws Exception {
client.resource(url).path(String.format(pathFormat,path))
。 queryParam(authParamName,credentials).delete(String.class);




$ p $需要以下依赖关系

 < dependency> 
< groupId> com.sun.jersey< / groupId>
< artifactId>球衣客户端< / artifactId>
< version> 1.19< / version>
< /依赖>

结构如下:

 客户端客户端= Client.create(); 
sdk = new FirebaseSdkJersey(url,credentials,client);



其中 url 是Firebase网址和 credentials 是在管理控制台中生成的密钥。


I need to be able to update some data to firebase without setting firebase to online so only the specified data gets sent while no wifi is available. I have been unable to find any documents or guides on how to use the REST API in Java. Can someone help me with it?

解决方案

Unfortunately there isn't an existing Java SDK for Firebase REST interface (that I could find). Instead you'll need to make one using your HTTP client of choice.

Here's an example using Jersey:

import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.vnomicscorp.firebase.sdk.FirebaseSdk;
import com.vnomicscorp.firebase.sdk.FirebaseSdkException;

public class FirebaseSdkJersey {

    private static final String DEFAULT_AUTH_PARAM_NAME = "auth";
    private static final String DEFAULT_PATH_FORMAT = "%s.json";

    private final String credentials;
    private final String url;
    private final Client client;

    private String authParamName = DEFAULT_AUTH_PARAM_NAME;
    private String pathFormat = DEFAULT_PATH_FORMAT;

    public FirebaseSdkJersey(String url, String credentials, Client client) {
        this.url = url;
        this.credentials = credentials;
        this.client = client;
    }

    public void setValue(String path, String value) throws Exception {
        client.resource(url).path(String.format(pathFormat, path))
                    .queryParam(authParamName, credentials)
                    .type(MediaType.APPLICATION_JSON).entity(value)
                    .put(String.class);
    }

    public String getValue(String path) throws Exception {
        return client.resource(url).path(String.format(pathFormat, path))
                .queryParam(authParamName, credentials).get(String.class);
    }

    public void deleteValue(String path) throws Exception {
        client.resource(url).path(String.format(pathFormat, path))
                .queryParam(authParamName, credentials).delete(String.class);
    }
}

Needs the following dependencies

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19</version>
    </dependency>

Construction looks like this:

    Client client = Client.create();
    sdk = new FirebaseSdkJersey(url, credentials, client);

Where url is the Firebase url and credentials is a secret key generated in the admin console.

这篇关于如何在Java中使用Firebase REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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