【问题标题】:create data model for this JSON file in Android在 Android 中为此 JSON 文件创建数据模型
【发布时间】:2017-02-16 21:36:04
【问题描述】:

我目前开始在 android 中使用 Firebase,所以我在创建数据模型时遇到了问题。所以请帮助我从 android studio 创建低于 JSON 的数据模型。

喜欢:

Firebase ref = new Firebase(Firebase_Url);
ref.child("Student").setValue(anyVariable);

此代码在 firebase 中生成一个简单模型并存储数据,但我想存储更多数据,如下所示。

你们的任何帮助将不胜感激。

"restaurants" : [
{
    "name": "Burger Bar",
    "backgroundImageURL": "http://somthing.com/Images/1.png",
    "category" : "Burgers",
    "contact": {
        "phone": "1231231231",
        "formattedPhone": "(123) 123-1231",
        "twitter": "1twitter"
    },
    "location": {
        "address": "5100 Belt Line Road, STE 502",
        "crossStreet": "Dallas North Tollway",
        "lat": 32.950787,
        "lng": -96.821118,
        "postalCode": "75254",
        "cc": "US",
        "city": "Addison",
        "state": "TX",
        "country": "United States",
        "formattedAddress": [
            "5100 Belt Line Road, STE 502 (Dallas North Tollway)",
            "Addison, TX 75254",
            "United States"
        ]
    }
} 

【问题讨论】:

  • 你尝试过做什么?这真的与 firebase 相关还是只是将 JSON 反序列化为模型对象?
  • 你使用 Android Studio 吗?
  • 我的 android 应用程序数据库模型与此代码非常相似,我想创建模型并将数据存储在 firebase 中。
  • 是的,我正在使用安卓工作室
  • 只是我不知道如何通过在 android studio 中编码以上层关系格式将数据存储在 firebase 中

标签: android json data-modeling


【解决方案1】:

我找到了解决方案:)

解决方案:

1) 在 Android Studio 中新建一个项目 2) 更改数据库规则,无需认证即可访问。喜欢

enter image description here

3) 首先,您必须在 Firebase 控制台创建新项目。

4) 在 build.gradle(Module:app) 中添加这个依赖

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

【讨论】:

    猜你喜欢
    • 2021-02-25
    • 2015-11-19
    • 2023-03-08
    • 2020-01-22
    • 2015-10-08
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多