Setting Up Manager

This lesson walks you through the explicit steps of creating a ContentManager, to allow you to supply your own custom behavior. This lesson also describes the recommended practice of creating a ContentManager as a singleton, which makes the ContentManager last the lifetime of your app.

Set Up a Network and Cache

A ContentManager needs only one basic component to get data from a web service: a network to transport the requests. It may optionally use another component to handle caching of the data.

There are out of the box implementations of these components shipped in the API: * HttpNetwork provides a network transport based on your preferred HTTP client, * FileCache provides a one-file-per-response cache with an in-memory index.

HttpNetwork is NeoMAD’s default network implementation.

This snippet shows you the steps involved in setting up a ContentManager:

public class MyApplication extends Application {

        protected void onCreate(Controller controller) {
                // Instantiate the ContentManager
                ContentManager myContentManager = new ContentManager();

                // puts cache and network instances into the manager
                File cacheFolder = new File(FileStorage.getCacheDir(), "subDir");
                myContentManager.putCache("MY_CACHE", new FileCache(cacheFolder, 1024 * 1024)); // 1MB cap
                myContentManager.putNetwork("MY_NETWORK", new HttpNetwork());

                // Sets the contentManager for all screens
                registerContentManager(myContentManager);

        }
}
public class MyScreen extends Screen {

        protected void onCreate() {

                // Formulate the query.
                String url = "http://www.example.com";
                ContentQuery query = new ContentQuery(Method.GET, url);

                // Set a response type to be able to distinguish responses in onContentResponse(ContentResponse)
                query.setContentResponseType("EVENT_EXAMPLE");

                // Add the query to the ContentManager.
                postQuery(query);

                // ...

        }


        // Handle the response
        @Override
        protected void onContentResponse(ContentResponse response) {
                if (response.hasType("EVENT_EXAMPLE")) {
                        // Do something with the response (as no parser is set, the value is returned as a byte-array)
                        byte[] data = (byte[]) response.getValue();
                }

        }

        @Override
        protected void onContentError(ContentError error) {
                // Handle error
        }

}

Set up Parsers

In the previous sample, no parser was set up. In such a situation, the raw data is returned from response.getValue(), i.e. a byte-array. As often, the web services response is returned using standard formats, like XML or JSON, that usually represent data models. Using the API, one can easily create a parser that will map the raw byte-array data to a more high-level data model. See here for more details