user2720380
user2720380

Reputation: 91

How to save data locally once get from API in android?

I'm doing Android app using web services. As of now each time getting response from server. If network fails i can't to get data. Activity showing empty screen. Instead of showing empty screen i want to save all data locally.

If network fails i want to get it from locally and show.

How to do that. Please share your valuable ideas.

IOS have core data to save values. Is Android have any option like that

Upvotes: 2

Views: 3612

Answers (3)

MobileMon
MobileMon

Reputation: 8661

Use a custom content provider (which is built on top of sqlite). Many tutorials on the internet describing how to do so

Content provider is similar to core data as they both place a wrapper on sqlite

Upvotes: 0

Sandip Lawate
Sandip Lawate

Reputation: 456

Create sqlite databse save all data u fetching from server.

SQLiteDatabase db = getWritableDatabse();

ContentValues contentValues = new ContentValues();
contentValues.put("ID", offlineData.getKey());
contentValues.put("RESPONSE_JSON", offlineData.getJson());
contentValues.put("LAST_UPDATED_TIME",
        String.valueOf(offlineData.getUpdatedTime()));
if(recordId==-1) {
    recordId = db.insert(TABLE_NAME, null, contentValues);
}else{
    recordId = db.update(TABLE_NAME, contentValues, "ID " + "="+offlineData.getKey(), null);
}
db.close();

Upvotes: 1

Shivaraj Patil
Shivaraj Patil

Reputation: 8364

If you mean when your app fetches the data for the first time you want to store it locally and when it fails next time you want to show locally stored data. If i am right

You can store the fetched data into MySqlite database. android Database Tutorials

and note that whenever new data is reveveid from web service update it locally. Else you will have old data in your database.

Upvotes: 0

Related Questions