Read Large JSON Files Using Gson

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

JSON exists everywhere and used mainly in REST APIs, There are many ways to read JSON files but here I will cover the two ways that are supported by Gson library.

  • Object Model

Load the whole file in memory and translate each JavaScript object to a Java object containing all the properties, array objects, etc. It is similar to the DOM way to read XML files.

  • Stream Model

Operate on a JSON document as a sequence of tokens that are traversed in depth-first order. Because stream mode reads one token at a time it requires less memory. It is similar to Pull Parsers way to read XML files.

When to use each model depends on your needs, If your JSON file is small then using object model to load the whole file into memory at once will be best choice, But if your JSON file it too large then using stream model will be the best.

I was having a large JSON file (≈ 30 MB) that I wanted to read. I couldn’t read the whole file at once using object model because loading all this data into memory made the system crash, So here I will describe how to use GSON library to read this file using stream model.

Firstly, Let’s take a look how the JSON file formed

cities_json

City and Coord model classes

city_model

coord_model

Now we will read the JSON file from assets folder using stream model

read_stream_model

With this approach we are loading objects one by one, and each time we convert the object that has been read to City model object.


References

What Is Android Intent ?

I was reading about Android Intents and I found this explanation very simple.

​An Intent is an “intention” to perform an action; in other words,

a messaging object you can use to request an action from another app component

An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly. Think of it as a blast email to a bunch of friends, in which you tell your friend John to do something, or to friends who can do X (“intent filters”), to do X. The other folks will ignore the email, but John (or friends who can do X) will react to it.

To listen for an broadcast intent (like the phone ringing, or an SMS is received), you implement a broadcast receiver, which will be passed the intent. To declare that you can handle another’s app intent like “take picture”, you declare an intent filter in your app’s manifest file.

If you want to fire off an intent to do something, like pop up the dialer, you fire off an intent saying you will.


References

Android Developer Path

Udacity has launched an Android Developer Path Guide. If you are newbie or have some experience with Android, take a look to that map to know the skills that you lack.

There are 3 levels in Udacity for Android developers:

  1. Beginner
    1. Android Basics: User Interface
    2. User Input
    3. Multi-Screen Apps
    4. Networking
    5. Data Storage
  2. Intermediate
    1. Developing Android Apps
  3. Advanced
    1. Advanced Android App Development

References

android-developer-path

Share Android App Internal Storage Data

Two weeks ago I faced a problem, I was storing a pdf file in internal storage and I wanted to share it via another apps like Gmail, or any other sharing app. When I tried to attach it to Gmail for example I got this error “Permission denied for the attachment“.
The problem is that data saved in internal storage isn’t accessible by other applications.

After a while I figured it out, that the solution is in Content Providers.

Content Providers have a lot of uses one of them is providing a way to share data with other apps.

In manifest we declare the content provider :

manifest-provider

name attribute is my custom content provider class, authorities attribute is URI that identify data offered by the content provider, exported is true means that the content provider is available for other applications to use.

After that we create our CustomContentProvider :

custom-content-provider

Finally, We will use our custom content provider to share our pdf file :

sharing

CONTENT_URI means I am going to use content provider “content://” + provider’s_authority + “/” + absolute file path.

That will allow our app to share the pdf file.

Linking Activities Using Intents

An Android application can contain zero or more activities. When your application has more than one activity, you may need to navigate from one activity to another. In Android, you navigate between activities through what is known as an intent.

Today i will talk about how to navigate from your current activity to another one. We will create new android project.

Then we will add new Activity by right click on package name under src folder then choose New –> Other –> AndroidActivity, And give it a name (I will name it “Activity2”) and press finish button.

After that we should add this activity to our AndroidManifest.xml … We will add those lines

And as we said before @string used to add our program strings in one file called strings.xml .. So we will simply go to strings.xml file and add this line

Activity2 is the title of our new Activity .. or you can name it as you like 🙂

Then we go to our res –> layout –> right click –> New –> Other –> Android XML File … ( I will name it activity_activity2.xml ) … then add this code to it.

Return back again to strings.xml to add txtactivity by adding this line

We go to our first activity xml ( main.xml file ) and create a button by adding this code …

And as we do before go to strings.xml and add a text for button like this

Then go to our MainActivity.java class to link your button in .xml with your button in .java class, and after that when button pressed we write the code that enable us to navigate to another Activity.

Run your code and enjoy 😀 .. This is the first screen

After you press the button .. The second Activity will be shown

That’s all for now, and i hope it’s easy to understand … if you have any question don’t hesitate 🙂

Activites and Intents – Part 3

This is the third lesson in our series of Activities And Intents … Now we will learn how to make a Progress Dialog … A progress dialog is useful for showing the progress of some activities, such as the status of a download operation.

The first thing we will create a button in our main.xml as we do in the previous tutorial …

As we do before we will go to our .java class and override onCreateDialog() method that will be called when we call showDialog(int id) method. Create an instance of the ProgressDialog class and set its various properties, such as icon, title, and style. And set the two buttons Hide and Cancel.

Then we go to onCreate() method and connect our button in .xml  with our button in .java code .. and then implement the function that when button pressed is fired .. and inside it we will call showDialog(int id) method and give the dialog a unique id. To display the progress status in the progress dialog, you need to use a Handler object to run a background thread

The background thread counts up to 100, with each count delayed by 100 milliseconds. When the count reaches 100, the progress dialog is dismissed.

Now Run your program .. and see your great result .. tadaaaaaa 😀

That’s all for now .. and sorry for my delay in publishing this lesson 🙂

Activites and Intents – Part 2

Okay now we will continue our last lesson .. in Today’s lesson the first thing that we gonna learn is how to display a dialog window .. ?!

First we will create a button in our UI (main.xml file) ..

android:id

This provides a unique identifier for the view, which you can use to reference the object from your app code, such as to read and manipulate the object (you’ll see this in the next lesson).The at sign (@) is required when you’re referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (btn_Show).The plus sign (+) before the resource type is needed only when you’re defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project’s gen/R.java file that refers to the Button element. Once the resource ID is declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts.

wrap_content value specifies that the view should be only as big as needed to fit the contents of the view.

fill_parent then the Button element would fill the screen, because it would match the size of the parent LinearLayout

The next step .. we will go to our .java class and write the code that respond to click event on our button .. Let’s first descripe the shape of our Dialog that we will create .. our dialog will contain a small picture, a word beside it, some check boxes and two buttons.

First we will create an array to contain check boxes’ names, and another array to contain a boolean that indicate if this item is checked or not .. then we will do as we do before .. we will connect our button in .xml file with our button in .java code .. and then implement the function that when button pressed is fired .. and inside it we will call showDialog(int id) method and give the dialog a unique id .. 🙂

Because we will show a dialog on our screen .. so we need to override onCreateDialog() method that will be called when we call showDialog(int id)  method. So now we will write this code in our onCreateDialog() method

The preceding code sets the Dialog Icon, Title and two buttons: OK and Cancel, using the setPositiveButton() and setNegativeButton() methods, respectively. We also set a list of checkboxes for users to choose via the setMultiChoiceItems() method. For the setMultiChoiceItems() method, you passed in two arrays: one for the list of items to display and another to contain the value of each item to indicate if they are checked. When each item is checked, you use the Toast class to display a message. A toast is a view containing a quick little message for the user. When the view is shown to the user, appears as a floating view over the application. And now when i run the App … Jerry Icon appears (i love tom & jerry :D)

The last thing i want to say the meaning of MainActivity.this that we use it as a parameter in Toast.makeText … On Android, a Context is used for many operations but mostly to load and access resources .. and MainActivity.this returns the current context of the activity, if activity is destroyed then it will be destroyed also.

That’s all for now … wait the other parts of Activities and Intents series 🙂