0

After successfully JSON parser stored into SQLite for offline storage after that retrieve the data from SQL lite when the internet is not available in android. Anyone tells me what is the process to do store JSON data into SQLite.

I am using Expandable listview for two model one is header and child model. how to create sqlite by using this Model.

Header Model:

import java.util.ArrayList;

public class Item {
    private String mainProductName;
    private String productImage;
    private ArrayList<SubItem> subItems;
    private String productid;

    public ArrayList<SubItem> getSubItems() {
        return subItems;
    }

    public void setSubItems(ArrayList<SubItem> subItems) {
        this.subItems = subItems;
    }

    public String getMainProductName() {
        return mainProductName;
    }

    public void setMainProductName(String mainProductName) {
        this.mainProductName = mainProductName;
    }

    public String getProductImage() {
        return productImage;
    }

    public void setProductImage(String productImage) {
        this.productImage = productImage;
    }

    public String getProductid() {
        return productid;
    }

    public void setProductid(String productid) {
        this.productid = productid;
    }
}

Child Model:

public class SubItem {
    private String inProductId;
    private String vcProductName;


    public String getInProductId() {
        return inProductId;
    }



    public void setInProductId(String inProductId) {
        this.inProductId = inProductId;
    }

    public String getVcProductName() {
        return vcProductName;
    }

    public void setVcProductName(String vcProductName) {
        this.vcProductName = vcProductName;
    }
}

Glad to appreciate Thanks

Nivethitha
  • 91
  • 1
  • 3
  • 16
  • 1
    Your question is unclear – Fiil Apr 27 '16 at 06:10
  • i want to check if i internet connection is available or not.if internet connection is on get the data from json url to display in my list otherwise internet is not available fetch the data from sqlite db.i need to store data in to sqllite after parsing json successfully. – Nivethitha Apr 27 '16 at 06:17
  • You can store the json string in db if that is what you are asking. And then you can use GSON to convert it back to your java classes. I can paste some code if you need – drulabs Apr 27 '16 at 12:28
  • i used volley can convert json in to string stored whole data in to sqlite – Nivethitha Apr 28 '16 at 04:11

3 Answers3

0

First: You read tutorial sqlite

This is tutorial SQLite

Then: After successfully JSON parse. Insert data to SQlite

Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22
0

If you need only a temporary storage of data then its better to cache the parsed data instead of writing it to data base. Refer this question and answer simple caching of data using Volley networking library. Android Setup Volley to use from Cache

Community
  • 1
  • 1
Eldho Paul Konnanal
  • 500
  • 1
  • 4
  • 23
  • then how to retrieve the data from cache – Nivethitha Apr 27 '16 at 08:54
  • If you followed the above example,then try loading data from server once and then switch the phone to aeroplane mode. shut the app down and reopen it. You'll see the data previously loaded is still there. No data loss. – Eldho Paul Konnanal Apr 28 '16 at 05:02
0

You can use Realm as database and Gson to parse your objects into POJO's. I think it's the fastest way to do it.

Tofasio
  • 425
  • 3
  • 14