Recently I realized that there are many questions regarding my “Develop your own Magento mobile application” article, and many new articles need to be written to answer them all properly and to guide you further through developing this kind of an application.
That is why I would like to present you an Android application, developed for the purpose of utilizing Magento’s XMLConnect and exploring possible modularity of the future application – mainly only a concept.
or for download
Let’s take a look at it. While examining folder structure you may notice that project is divided into several packages. Also, very important is the imported java library SimpleXML, we’ll go through it in a bit.
The main idea was to develop an app that will hold all the essentials for communication with a Magento store, handle persistency (session) and provide tools for caching. Not all is yet developed (like caching), but this alone will give you more than enough to start your own project.
- an entry of the app, holds the only Activity
- holds all the basic functions to allow communication with Magento store, handles persistency in session, also holds essential configuration and customer models needed throughout the app
- a separate module for loading / handling application configuration from the Magento store
- comes with it’s own dataset package com.ivica.magentoid.configuration.ds
- a customer module, will house all the customer related functionality – at this moment doesn’t have it’s own dataset package
I believe code is a good enough documentation itself so I won’t go into too much detail here, but would like to talk about deserialization and datasets, crucial for mobile app – Magento store communication.
Deserialization – DataSets
Parsing XML data is something most developers, including myself, do not enjoy one bit. There are various approaches to parsing, but I wanted not to think about plain string and wanted only to work with solid objects. That’s where SimpleXML, a great serialization / deserialization library, comes handy.
Basically, SimpleXML provides a serializer class witch you feed with your (dataset) class and an input stream (string) for parsing. As a result it returns a new object based on the class you provided and populated with data from the provided stream.
// ConfigurationDS ds; ds = serializer.read(ConfigurationDS.class, anInputStream); //
What is and how to create a dataset class? To put it simply, a dataset class is a plain java class with fields which are supposed to store values from an XML string, and with annotations all over the place which tell SimpleXML serializer how to do it.
I won’t go into details about SimpleXML annotations here because it’s a story for itself, but you may find out on its site that it is pretty well documented.
Ready, Set, Action!
If you remember in my previous article we checked Magento mobile application’s configuration by navigating to:
(change server name and app code accordingly)
As a result we see an entire application configuration as it is setup in Magento admin, in an XML format. This is what we feed our serializer with.
Before you run the provided app, be sure to set your Magento store’s url and application code in
com.ivica.magentoid.core.MagentoidApp.java class file.
// public static final String MAGENTO_URL = "http://magentoivica.loc"; public static final String MAGENTO_APP_CODE = "defand1"; //
Now, if you run the app you will see a copyright message popping up in a Toast, as it is set in your Magento store.
It’s as simple as
// Toast.makeText(this, mApp.getConfiguration().ds.getGeneral().getCopyright(), Toast.LENGTH_LONG).show(); //
(notice how it ‘follows’ structure of the XML result above)
I hope this will answer some of your questions about the subject and help you with your project.