Я нашел решение :)
решение:
1) Создайте новый проект в Android Studio 2) Изменение правил базы данных для доступа без аутентификации. как
enter image description here
3) Во-первых, вы должны сделать новый проект на Firebase консоли.
4) Добавить эту зависимость в build.gradle (Module: приложение)
compile 'com.firebase:firebase-client-android:2.4.0'
5) добавить новый класс Restaurants.java
public class Restaurants {
String name;
String imageUrl;
String catagory;
Contacts contacts = new Contacts();
Location location = new Location();
public Restaurants(String name, String imageUrl, String catagory, Contacts contacts, Location location) {
this.name = name;
this.imageUrl = imageUrl;
this.catagory = catagory;
this.contacts = contacts;
this.location = location;
}
public String getCatagory() {
return catagory;
}
public void setCatagory(String catagory) {
this.catagory = catagory;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Contacts getContacts() {
return contacts;
}
public void setContacts(Contacts contacts) {
this.contacts = contacts;
}
}
6) создать класс Contacts.java
public class Contacts {
String phone, formattedPhone, twitter;
// 0 argument constructor to initialize the Contatcs Object in Resturants.java
public Contacts() {}
public Contacts(String phone, String formattedPhone, String twitter) {
this.phone = phone;
this.formattedPhone = formattedPhone;
this.twitter = twitter;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getFormattedPhone() {
return formattedPhone;
}
public void setFormattedPhone(String formattedPhone) {
this.formattedPhone = formattedPhone;
}
public String getTwitter() {
return twitter;
}
public void setTwitter(String twitter) {
this.twitter = twitter;
}
}
7) Создать класс Location.java
public class Location {
String address, city, state, country;
ArrayList<Address> addressList;
public Location(){}
public Location(String address, String city, String state, String country, ArrayList<Address> addressList) {
this.address = address;
this.city = city;
this.state = state;
this.country = country;
this.addressList = addressList;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public ArrayList<Address> getAddressList() {
return addressList;
}
public void setAddressList(ArrayList<Address> addressList) {
this.addressList = addressList;
}
}
8) Создать класс Address.java
public class Address {
String street, area;
// 0 argument constructor to initialize the Address Object in Restaurants.java
public Address(){}
public Address(String street, String area) {
this.street = street;
this.area = area;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
9) Теперь MainActivity.java класс будет как этот
public class MainActivity extends AppCompatActivity {
ArrayList<Restaurants> restaurantsList;
ArrayList<Address> addressList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
//Initializing the ArrayLists
restaurantsList = new ArrayList<Restaurants>();
addressList = new ArrayList<Address>();
//Adding data to ArrayLists
addressList.add(new Address("Street 5", "Mohafiz Town"));
addressList.add(new Address("Street 6", "Wapda Town"));
restaurantsList.add(new Restaurants("Ehsan", "url1", "student", new Contacts("0303-5367228", "no", "my Twitter"),
new Location("202-A", "GRW","pak","Pakistan", addressList)));
//Storing Data to Firebase
Firebase ref = new Firebase("https://fir-datamodelingprac.firebaseio.com/");
ref.setValue(restaurantsList);
}
}
10) Запустите приложение
11) Выход в firebase будет таким:
enter image description here
Что вы пытались сделать? Это действительно связано с firebase или просто десериализацией JSON в объектах модели? – Darxval
Вы используете Android Studio? –
Моя модель базы данных для приложения для Android очень похожа на этот код, и я хочу создать модель и хранить данные в firebase. –