5

Okay so, I created an app that retrieves data from my server using JSON. Now I want to store the retrieved data on my phone's local storage/db. How do I do it? I am new in android programming.

This is the JSON that I receive from the server

{"messages":[{"id":"44","issender":0,"content":"CAT1DOG","date":"Jan 01, 1970 07:30 AM","sender":"Administrator","receiver":"User"},{"id":"57","issender":0,"content":"ttt","date":"Jun 30, 2016 03:43 PM","sender":"Administrator","receiver":"User"},{"id":"58","issender":0,"content":"s","date":"Jun 30, 2016 03:43 PM","sender":"Administrator","receiver":"User"},{"id":"82","issender":0,"content":"yeuwu","date":"Jun 30, 2016 04:59 PM","sender":"Administrator","receiver":"User"}],"success":1}

and this is my code to parse JSON

for(int i = 0; i < messages.length(); i++){
                        JSONObject o        = messages.getJSONObject(i);
                        String msgid        = o.getString("id");
                        String message      = o.getString("content");
                        String date         = o.getString("date");
                        String sender       = o.getString("sender");
                        String receiver     = o.getString("receiver");
                        String issender     = o.getString("issender");

                        // TEMP HASHMAP FOR USER
                        HashMap<String, String> msgList = new HashMap<String, String>();

                        // ADDING EACH CHILD NOTE TO HASHMAP => VALUE
                        msgList.put("id", uid);
                        msgList.put("message", message);
                        msgList.put("date", date);
                        msgList.put("name", sender);
                        msgList.put("receivername", receiver);

                        // ADDING USER TO MSGLIST
                        ListOfMsg.add(msgList);
                    }

Thanks in advance for those who will answers. will appreciate it.

Akio
  • 134
  • 1
  • 3
  • 13
  • Using SQLite database you can achieve – Sathish Kumar J Jun 30 '16 at 09:11
  • It is actually very easy. All you need to do is save parse your json into a java class (gson is a great way to do that). Then you could either manually save the java class using SQLite or you could use a third party library to save it for you. You can also save the json in the assets package. However, the best way to do that is to parse the json then use a library to save the data. If you want to proceed with this suggestion, I am more than ready to help – Miriana Itani Jun 30 '16 at 09:13
  • @MirianaItani I would consider your suggestion and will appreciate your help ^_^ – Akio Jun 30 '16 at 09:16
  • Possible duplicate of [How to ship an Android application with a database?](http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database) – Abhishek Jun 30 '16 at 09:17
  • @Akio please first edit the question to show the json u are receiving from the server. Then I am almost sure u are using android studio, so add compile 'com.google.code.gson:gson:2.4' to ur gradle file and let us get started – Miriana Itani Jun 30 '16 at 09:19
  • @MirianaItani will do – Akio Jun 30 '16 at 09:25
  • You can store the whole string in the SharedPreferences and then parse it later when you need it. – Vucko Jun 30 '16 at 09:52
  • @Vucko how will I do that? – Akio Jun 30 '16 at 11:31
  • Just google how to save a string in SharedPrefs. It's easy and you'll find tons of examples. – Vucko Jun 30 '16 at 12:50
  • Try ObjectBox https://objectbox.io/ – humble_wolf Jan 12 '19 at 14:58

2 Answers2

3

You can do this in two ways:

  1. Use the new extension for json in sqite. The information you might need is available on this page https://www.sqlite.org/json1.html . Still I would suggest to do a little bit more of research on this, as it is new and I have not used it yet.

  2. You can convert your json to string and insert it to the database.

    String jsontostring = jsonObject.toString();
    
Rishabh Lashkari
  • 638
  • 1
  • 8
  • 22
3

First I need to tell you that this is not the easy way out but for sure it is the correct one.

Next create a new class named Message

public class Message{
  public String issender;
}

When you receive the json :

List<Message> messages= new ArrayList<>();
Gson gson = new Gson();
Message m= gson.fromJson(json.toString(),Message.class);
messages.add(m);

Please be careful that the items in the class should have the name as the items in the json you are trying to receive

Now we are done with this part: Let us add the library for caching: Follow this tutorial and if you need help get back to me: https://guides.codepath.com/android/activeandroid-guide

or you could do the caching using sql old fashioned way

Miriana Itani
  • 865
  • 9
  • 25