Sunday, December 19, 2010

Reading a remote XML file with SAX

If you need to read an remote XML file you can use SAX, DOM or the parser you want. I have tested it with SAX approach.

The first thing you need is give permission to your app for accessing Internet. So, in AndroidManifest.xml, you should add the next line:

<uses-permission android:name="android.permission.INTERNET"/>

I will read the next location, in order to get the different currency values published by European Central Bank:

Let's begin with SAX example. I have made two classes: CurrencyXMLReader and CurrencyXMLHandler.

CurrencyXMLReader will be in charge of create SAXParserFactory,
CurrencyXMLHandler and read the remote XML file, as follows:

public class CurrencyXMLReader
   private String urlM;
   public CurrencyXMLReader( String theUrl) 
      urlM = theUrl;

   public CurrencyList obtainCurrencyList() throws ParserConfigurationException, 
      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
      SAXParser parser = parserFactory.newSAXParser();

      XMLReader xmlReader = parser.getXMLReader();
      CurrencyXMLHandler currHandler = new CurrencyXMLHandler();
      xmlReader.setContentHandler( currHandler);
      URL url = new URL( urlM);
      xmlReader.parse(new InputSource( url.openStream()));

      return currHandler.getCurrencyList();        

where CurrencyList is a class wich will contain the list of currencies retrieved from Bank and the date of the data read. It will have no special interest for the purpose of this post.

XML file has the next content:

   <gesmes:subject>Reference rates</gesmes:subject>
      <gesmes:name>European Central Bank</gesmes:name>

      <cube time="2010-12-17">
         <cube currency="USD" rate="1.3260"/>
         <cube currency="JPY" rate="111.26"/>
         <cube currency="BGN" rate="1.9558"/>

And I am interested in the date of currency data and in the currencies and their values.

CurrencyXMLHandler will be in charge of process the data of XML we have read:

public class CurrencyXMLHandler extends DefaultHandler{
   private CurrencyList currencyListM = new CurrencyList();
   public void startElement( String theNamespaceURI, 
                             String theLocalName,
                             String theQName, 
                             Attributes theAtts) throws SAXException 
      if( theLocalName.equals("Cube"))
         if( theAtts != null)
            if( theAtts.getValue( "currency") != null)
               String currency = theAtts.getValue("currency");
               String rate = theAtts.getValue( "rate");
               currencyListM.add( new CurrencyData( currency, rate));
            else if( theAtts.getValue( "time") != null)
               currencyListM.setDataDate( theAtts.getValue("time"));
   CurrencyList getCurrencyList()
      return currencyListM;

Console traces for debugging applications

One of the first things we usually need when we are developing an application is to insert log traces. In Android Stack System.out.println standard function doesn't works. If we need to see traces on Android console we must use the android.util.Log package.

We have different methods depending on the trace level we use:
  • VERBOSE: Log.v()
  • DEBUG: Log.d()
  • INFO: Log.i()
  • WARN: Log.w() 
  • ERROR: Log.e()

We can use a TAG for each class, for each activity or for the whole system in order to group the traces:

private static final String TAG = "MyClass";

So, debug methods can be used as follows:

Log.d( TAG, "String to be traced");  

We don't need to do any more.

UPDATE: I have found some applications for the smartphones which allow to see, filter by tag or level, save on SD and e-mail all the system traces. Here you have some of them:

Dealing with a Client/Server application

This is the first post of two different series of publications, which will focus on developing client-server applications to the field of smart phones with Android.

To address this challenge, there are plenty of approaches, frameworks and tools.
We chose a stack of items based on the following criteria:
  • Productivity: We are looking for a platform on which development is very fast.
  • Ease of use: We want languages and frameworks easy to learn, set...
  • Low cost: We will not licensing costs, rental of hosting...
If we consider other types of selection criteria, we would have made different choices, but with these in mind the selection for the server side is the next stack of frameworks:

  • Google AppEngine: is the hosting provided by Google. The main advantages are that it is a cloud computing solution that provides scalability, easy to use, and is completely free, for a number of hardware resources are enough for an application with medium traffic. On the hand, the main disadvantage if we choose the Python option within the Google AppEngine, is that the database provided is not a relational DB and therefore lacks certain basic features like a simple Join.
  • Django: This is one of the possible web development frameworks that exist for Python. The main feature is its simplicity and productivity in development.
  • Django-nonrel: Django is an adaptation of Django, in order to run on non-relational BD AppEngine.
  • Wadof Stuff JSON Serializer: Although Django comes with standard JSON serializers, we will add this optional plugin for Django to get better features. For example the serialization of non-persistent attributes or relationships between entities.
The first series of posts will deal with the server side, and the second part is about developing the client side, over the android smartphone.

Sunday, December 12, 2010

Taking screenshots without rooting the phone

If you need to send one screenshoot of the application you are developing, you have two possible ways:

  • Root your phone and use one application from market
  • Use ddms SDK tool

Using ddms tool is very easy (you must have installed the SDK version corresponding to your phone):

  1. Plug in your phone in PC and select PC Mode (HTC Sync in my device)
  2. Exec ddms tool. You can find it in the tools directory of your Android SDK installation.
  3. In ddms tool select your phone and in "Device" menu select "Capture Screen"

Now, you should have it.

But if you are using Ubuntu as OS, perhaps you may need to grant access to the phone by adding a new UDEV rule:

  1. Open a console terminal
  2. You have to find out the Vendor ID of your device. Type sudo lsusb -v and search in the output the entry corresponding to your phone. Also, you can find it in this web:
  3. Create a file for the new UDEV rule: sudo vi /etc/udev/rules.d/90-android.rules
  4. In new file add the next line (0bb4 is my Vendor ID): SUBSYSTEM=="usb", ATTRS{idVendor}=="0bb4", MODE="0666"
  5. Restart the UDEV service sudo service udev restart
  6. Unplug and plug the phone again

Enjoy it.

Help for developers

Today, I have thought in searching apps and resources in the Android Market for developers. I have found one I think is interesting. It is called Google Dev Helper 2010. It has documentation and videos about Android, Chrome, Google API's and many other things. Install it and take a look.

Wednesday, December 8, 2010

Common views, layouts and widgets

Today I have been very obfuscated with the controls I should use for an little example application. A good steep for beginners in Android knowledge is to know basic views, layouts and widgets, as well as the graphical representation of them. I recommend you this link from Android SDK documentation. It is in the "Hello views" tutorial menu:

The solution for my little problem is the spinner widget.

The first post

Hi everybody,

this is the first post for this blog. This blog has been opened  by a group of friends that have begun to learn to code with Google Android SDK. This doesn't pretend to be a reference guide for programmers. There were other blogs and of course, the Android SDK documentation for that purpose. Here, we will post the problems we are going to find during our learning and issues we can consider of special interest from our point of view. We hope this can be interesting for you.

Best regards.