3

I'm building an Android app, which should:
show some data, loaded from a server in the Internet.

At the moment I have a local SQliteDB used in my app where the data is stored, which should be displayed. I use this, because I want to be able to show the data, even if there is temporarily no internet connection available.

Next step I will work on inserting data in the local SQliteDB from a internet server. I thought about doing it this way:

When app starts, check if internet is available. If yes, connect to a webservice (including username and password). The webservice should deliver the necessary data via json object to the app and I will update the local SQlite DB.

My questions:
Is this a good idea?
Are there any better ways to do this?

The data can be viewed (and edited) by a Zend Website, too.

Thanks in advance.

Best regards Daniel

Shashwat Black
  • 992
  • 5
  • 13
  • 24
user3460622
  • 165
  • 1
  • 11

3 Answers3

3

The way you put it seems optimal. Maybe you should set a flag or alert which is time or date related..in case the app starts too many times without internet.

>> For updates to your mobile app, you should consider the priority/urgency of having the same data on the server and your app.

> For the better ways to do it, you can opt the way which suits your requirement better.

To fetch the data in one thread and render it in another,

1. Write custom Asynctasks:
http://developer.android.com/reference/android/os/AsyncTask.html
AsyncTask Android example

OR

2. Use something like AsyncHttpClient: http://loopj.com/android-async-http/
where you get onSuccess and onFailure methods to work with the response.

The option 2. is better if you just want to fetch data without doing anything else, and work on it, or save it. For the same, you need to parse the response first.

To parse your data:
As your response is JSON format, you may be better off using Gson to map data and with custom model classes. Eg.:

    Gson gson = new Gson();
    ModelClass modelClass= new ModelClass();
    modelClass= gson.fromJson(responseContent,ModelClass.class); 
//where responseContent is your jsonString
    Log.i("Web service response", ""+modelClass.toString());

More on: https://code.google.com/p/google-gson/

For Naming discrepancies(according to the variables in webservice), can use annotations like @SerializedName.

Use a for each loop to verify/browse/access the data that would be populated in/as objects/fields of your model class:
Check these for doubts:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?

Now about saving your data:

>> It depends a lot on what the data from server is and how much data do you want to store.

In Android Storage Options:
http://developer.android.com/guide/topics/data/data-storage.html

a. There's Shared Preferences:
These are good for saving/storing data which would be relatively small in size and could be overwritten and fetched frequently. Eg. username, current user's details, password
http://developer.android.com/reference/android/content/SharedPreferences.html
How to use SharedPreferences in Android to store, fetch and edit values

b. Maintaining a database is good for the larger chunk needed in your app.
You can store, update or over-write the data according to your need. There can be multiple tables or more data could be stored in various fields.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html

While you use Gson, you also have the option of populating the objects of model class and then storing that response in a String(maybe in SharedPreferences, depending on length/size) using gsonToJson method. I have used that, cheeky but effective.

You need to consider other stuff too, pertaining to UI and memory optimization, ListViews or layouts etc depending on your app and its control flow.

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
0

You could start a thread and get new data on loading the app. If you decide this path we had nice results with JSON using the Volley Project.

You can see a walk through here http://www.javacodegeeks.com/2013/06/android-volley-library-example.html

and get the code here https://android.googlesource.com/platform/frameworks/volley/

ndgreen
  • 167
  • 3
  • 8
0

Try using this library:- https://github.com/Abhishekpalodath/FetchServerUtil-Android.git. You can also load real-time data from the server by using this.

Abhishek c
  • 539
  • 6
  • 16