You are here

Add the basic code

This task describes how to add the basic code changes.
The code is essentially the same as the HelloRepo, however, certain logging has been removed to make the code easier to work with.
  1. If you have not already done so, create an SDK application called TestApp1.
  2. In the Package Explorer expand TestApp1 and open the src folder.
  3. Expand to locate the MainActivity.java source file.
  4. Double-click to load the file into the Eclipse editor.
  5. Modify your code so that it is the same as the following:

    Note: The code is similar to what you saw in previous tutorials. In addition to some logging having being removed, the code also has the check for the session object removed, and simply quits if a connection cannot be established. The progress update stub method has also been removed. This has been done just to make the code a little easier to read and work with.
    Attention: Also, don't forget to edit the code to use the appropriate login details for your installation.
                  
    package com.alfresco.tutorials.testapp1;
    
    import java.util.List;
    
    import org.alfresco.mobile.android.api.exceptions.AlfrescoSessionException;
    import org.alfresco.mobile.android.api.model.Folder;
    import org.alfresco.mobile.android.api.model.Node;
    import org.alfresco.mobile.android.api.model.Site;
    import org.alfresco.mobile.android.api.services.DocumentFolderService;
    import org.alfresco.mobile.android.api.services.SiteService;
    import org.alfresco.mobile.android.api.session.RepositorySession;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Modify to suit your SkyVault server installation 
            String url = "http://localhost:8080/alfresco";
            String username = "admin";
            String password = "admin";
    
            new ConnectToRepo().execute(url, username, password);
    
        }
    
        class ConnectToRepo extends AsyncTask<String, Integer, String> {
    
            private static final String TAG = "ConnectToRepo";
    
            @Override
            protected String doInBackground(String... params) {
    
                Log.d(TAG, "doInBackground");
                Log.d(TAG, params[0] + ":" + params[1] + ":" + params[2]);
    
                String url = params[0];
                String username = params[1];
                String password = params[2];
    
                // HelloRepo
                try {
                    // connect to on-premise repo
                    RepositorySession session = RepositorySession.connect(url,
                            username, password);
    
                        // Get site service
                        SiteService siteService = session.getServiceRegistry()
                                .getSiteService();
    
                        // Get sites for current user
                        List<Site> sites = siteService.getSites();
    
                        // Get first site
                        Site site = sites.get(0);
    
                        // Get site document library
                        Folder folder = siteService.getDocumentLibrary(site);
    
                        // Find DocumentFolderService
                        DocumentFolderService documentFolderService = session
                                .getServiceRegistry().getDocumentFolderService();
    
                        // Get children of document library
                        List<Node> nodes = documentFolderService
                                .getChildren(folder);
    
                } catch (AlfrescoSessionException e) {
                    Log.e(TAG, "Failed to connect: " + e.toString());
                    System.exit(0);
                }
    
                Log.d(TAG, "doInBackground Complete");
                return "doInBackground Complete";
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Log.d(TAG, "onPostExecute");
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
            }
    
        }
    
    }                          
                  
                
  6. The activity_main.xml file needs to be modified too. The main layout can be a simple Linear Layout. This will be referenced from the main activity. In the Package Explorer expand the TestApp1 folder.
  7. Expand the res and then layout folders.
  8. Double-click on the activity_main.xml file to load it into Eclipse.
  9. Click the tab called activity_main.xml to view the XML source for the file, rather than the graphical layout.
  10. Modify the XML to match the following:

                  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
      android:id="@+id/linearLayout"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    </LinearLayout>              
                  
                

    The id will be used to access the layout from the main activity's code.

  11. Save everything and run your application to test it. It will connect to your repository without error.
At this point you have a basic working SkyVault SDK for Android application, that connects to the repository, reads some nodes and prints some minimal tracing information. In the following tasks you will expand upon this foundation, adding a GUI.