混合Firebase实时数据库

时间:2017-05-29 14:27:32

标签: android firebase firebase-realtime-database

我在android和ios都有一个应用程序,我可以使用firebase实时同步相同的数据吗?

我已经为两者制作了firebase项目,但是如何让ios和android指向一个firebase数据库,我尝试过getreferncefromurl()但是显示错误的网址不匹配

我也试过把getinstance(url)但dint起作用

代码: -

package com.example.rushabh123453.n123_real;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    TextView condition;
    Button sunny,foggy;

   /* String url="https://iosprojle-456.firebaseio.com/";
    FirebaseApp ref = new FirebaseApp(url);*/


    DatabaseReference mRootRef=FirebaseDatabase.getInstance().getReference();
    DatabaseReference conditionref=mRootRef.child("condition");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        condition=(TextView)findViewById(R.id.textView_cond);
        sunny=(Button)findViewById(R.id.button_sunny);
        foggy=(Button)findViewById(R.id.button_foggy);
    }

    @Override
    public void onStart()
    {
        super.onStart();

        conditionref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                        String text  =dataSnapshot.getValue(String.class);
                condition.setText(text);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


 sunny.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         conditionref.setValue("Sunny");
     }
 });

        foggy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conditionref.setValue("foggy");
            }
        });
    }
}

0 个答案:

没有答案