Using PHP on server side to generate JSON

Recently, I publish one application into the Android Market that tries to predict when Spain will default. The application uses the data provided by my colleague Juan Carlos Barba from his server. There are basically a set of levels pointing out the seriousness of the Spanish level of CDS and Spread of the debt. The model establishes 5 different levels of alert (or how he called them, DefCon). My implementation customized the data to visualize into the Android Platform.

Firstly, I needed to access his database into the server, which was a slow task due to the nature of the server (a personal computer with a normal DSL connection). My idea was to rely this task into something that could avoid overloading the server, and then I thought about everything that happens now with all the client / server application. Why not using a PHP file to generate JSON with the information I needed?

Since I do not have access to Juan Carlos’ server, I decided to do it on my own server. I created some PHP files that query the server, and give me the information in JSON format

 

Now the data has to be requested and handled from the Android side:

 
private void initPHP() {
		//http post
		 String result = "";
		  InputStream is = null ;
		 try{
			 DefaultHttpClient httpclient = new DefaultHttpClient();
		         HttpPost httppost = new HttpPost("http://www.neo-tech.es/defcon/cds.php5");
		         HttpResponse response = httpclient.execute(httppost);
		         HttpEntity entity = response.getEntity();
		         is = entity.getContent();
		 } catch(Exception e) {
		         Log.e("log_tag", "Error in http connection "+e.toString());
		 }
		 //convert response to string
		 try{
		         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
		         StringBuilder sb = new StringBuilder();
		         String line = null;
		         while ((line = reader.readLine()) != null) {
		                 sb.append(line + "\n");
		         }
		         is.close();
		  
		         result=sb.toString();
		 }catch(Exception e){
		         Log.e("log_tag", "Error converting result "+e.toString());
		 }
		  
		 //parse json data
		 try{
		         JSONArray jArray = new JSONArray(result);
		         int lastDay = 0;
		         for(int i=0;i

The code can be improved to handle authentication, HTTPS, or do more complex work in general. In a project at work we used authentication based on a pair of generated keys, but a simple method based on a known keyword is enough.

Why Android still sells less than Apple?

Android vs iPhone

As an Android developer, I have been concerned during the last months about why the Android Market seems to be less profitable than the App Store. I have access to some statistics of my application’s ports to iPhone/iPad: while the free applications don’t seem to be affected, the paid applications have a huge gap with their iOS counterpart: they are downloaded up to 50 times more. I only can feel that I chose the wrong mobile platform to develop for. But why is this happening?

There are a bunch of well known differences between the nature of iOS and Android applications, but not all of them affect the sells. According to the report of Gartner (April 2011), 38.5% of the smartphone market belongs to Android, while 19.4% belongs to iPhone. It is almost the double for Android, and the growth has been astonishing: from an estimated 3.9% in 2009, it has rapidly overtaken all of their competitors

Growing Graph

But if we look on the statistics, Apple’s iPhone App Store is still larger in size than the Android Market. It houses more than 350,000 applications and games, while it was reported in Business Insider in March 2011, that the Android Market has hit the 250,000+ mark. This obviously has as well a dark side: with many competing applications, is harder to sell our own application. Android does not have (yet) as many applications as the iPhone store, but it lacks of a Desktop client (iTunes) and it is much less user friendly (a field where Apple always wins).

Number of applications

So there we go: Android has sold more phones, and iOS has published more applications. One of the first thoughts is that the developers are choosing iOS for a misterious reason. I might point out here one of the main problems with Android: the fragmentation. Whereas Google provides the operative system, the different manufacturers provide the hardware. This leads into a huge uncontrolled mess: different phones may support (or not) different features (i.e., camera, giroscope, etc), which is a problem for developers.

In order to ensure a minimum of compatibility between many devices, Google Android has the Compatibility Program, an automated control mechanism that determines which device is minimally compatible with Android.

The devices that pass the program can use the official logo of Android, and what is truly important, they can integrate the application that gives access to Android Market, Android application repository.

Another reason could be the politics of payment and refund. Payments on the iPhone App Store are handled via your iTunes account which is linked directly to your credit card, whereas Android uses Google Checkout, a paypal clone of Google. Many people have an iTunes account since a few years, but Google Checkout is still relatively unknown (I didn’t have an account until I need it to get my revenues from the Android Market!).

Also one major difference between the Android Market and the iPhone App Store is refunds. With Android, you can test an application for 15 minutes and uninstall it in order to obtain a refund. However, this is not allowed in iPhone App Store. This results in greater sales in iPhone Store, enabling more revenue for the developers.

So what exactly is going wrong in the Android Market? In my opinion there are several reasons, first and most important of these is that the Android Market, even though its recent renovation earlier this year, it remains still below the design level of the App Store. And since we download through the device, we really need a good design and features to facilitate the user purchase.

Growing prediction

There are also rumours that Google promotes free and ad-supported applications instead of paid applications, that Android users belong to an economically lower profile than the iOS users, or that they did not have time to do things better. I want to believe that the main reason is a ponderate mix of all the aforementioned reasons, with a special weight of the last one: Android has been a special challenge for a company that never worked in mobility, and they still need a little bit of time to improve everything. They have already begin to improve the Android Market and they are doing a huge effort to solve the fragmentation problem. Amazon Store will also be relevant next year.

I think it takes a little but longer, but hopefully Android will eventually get off and will be as profitable for developers as the App Store it is today.

Some other links:

Language Assistant program for Android

For a long time, I used to manage the different language files in Android manually. This is an incredible boring task when our program is growing and getting more complex, since the only way to compare tokens is doing it one by one, and is an error-prone task.

This program is an extension to a similar program developed by my colleague Eugenio Marchiori at the e-UCM group. The program compares all the tokens between two different languages, assigning different codes depending if the token is missing in one language, or it is has been created. The task of creating new language files is also very easy, since they can be created using a full version as reference.

The program can be downloaded from this link. In the first screen, the parent folder of an Android project has to be selected. After that, you just can edit your language files!

UPDATE: Now the application has been released as a GPL application. You can download it from the Google Code repository.

Language Assistant for Android

New Intense Languages versions

Some time ago, I released my application Intense Esperanto. The learning method used in I EO has been proposed by Chuck Smith and Judith Meyer, and I developed the application in collaboration with them. Well, now I’m releasing more versions for more languages: this time, Intense German and Intense Latin. Those last versions are not free, but I guess the price is fair: it give me some extra-money to continue developing more applications, while Intense Esperanto and the others applications remain free.

logo

First application published in the Android Market

Recently I talked about my last personal project, Farver. After considering if it could be possible to get something more from the effort and the hours invested into it, I decided to port the application into the Android Market.

The application can be search through the word “Farver” in the Android Market. I have also created a video showing both the web and Android applications.

Farver screenshot