Build your own android RSS applications on web server


Inspired by OVI app wizard I tried creating a small webpage to create android application on the fly.

To do this I used apache tomcat 7 and simple servlet programming.

I used apache-tomcat setup guide for installation.

Now i had my server ready so I referred Developing Web applications with Tomcat and Eclipse and created a new tomcat project “AppOnFly” inside $CATALINA_HOME/webapps/

Normal web application has following content,

anshu@Anshu-Notebook:~/apache-tomcat-7.0.14/webapps/AppOnFly$ ls -lrt 
total 40 
drwxr-xr-x 2 anshu anshu 4096 2011-05-30 16:24 bin 
drwxr-xr-x 2 anshu anshu 4096 2011-05-30 16:24 work 
drwxr-xr-x 2 anshu anshu 4096 2011-05-30 16:24 src 
drwxr-xr-x 5 anshu anshu 4096 2011-05-30 17:15 WEB-INF 

Now I created another directory “android_project” inside project folder.

anshu@Anshu-Notebook:~/apache-tomcat-7.0.14/webapps/AppOnFly$ ls -lrt 
total 40 
drwxr-xr-x 2 anshu anshu 4096 2011-05-30 16:24 bin 
drwxr-xr-x 2 anshu anshu 4096 2011-05-30 16:24 work 
drwxr-xr-x 2 anshu anshu 4096 2011-05-30 16:24 src 
drwxr-xr-x 5 anshu anshu 4096 2011-05-30 17:15 WEB-INF 
drwxr-xr-x 3 anshu anshu 4096 2011-05-30 17:26 android_project 

Inside android_project i copied my own RssReader android application. This application has simple implementation of RSS parser and it takes the RSS url from channel.prop file in assets.

Now all i had to do was get the URL from webpage and create channel.prop file and build the application. To do this, my servlet came in picture.

But before getting into servlet, I added one more file, i.e., run.sh to run ANT to build my android application.

run.sh

 #!/bin/sh 
cd ../webapps/AppOnFly 
rm -r -f *.apk 
cd ./android_project/MyRssReader/ 
ant clean 
ant compile 
ant debug 
cp ./bin/MyRssReader-debug.apk ../../MyRssReader.apk 

Now I had everything except a webpage and a servlet.

I created a simple JSP page index.jsp

index.jsp

<html>

  <head>
    <title>AppOnFly</title>
  </head>
  <body>
    <h1>Create your own android RSS Reader</h1>
    <hr>
    <h2>Please enter the URL for rss feed that you want to follow</h2>
    <br>
    <hr>
    <form action="go" method="post">
    	<input type="text" name="url">Enter URL</input>
    	<input type="submit"/>
    </form>
  </body>
</html>

Now last two steps were to create a servlet(AppCreator.java) and web.xml

AppCreator.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 */

/**
 * @author anshu
 *
 */
public class AppCreator extends HttpServlet {

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter pw = resp.getWriter();
		pw.write("<html><body>");
		File propFile = new File(getServletContext().getRealPath(
				"android_project/MyRssReader/assets/channel.prop"));
		if (propFile.exists()) {
			propFile.delete();
			propFile.createNewFile();
		}
		FileOutputStream fout = new FileOutputStream(propFile, false);
		fout.write(req.getParameter("url").getBytes());
		fout.close();

		String cmd = getServletContext().getRealPath("run.sh");
		Runtime run = Runtime.getRuntime();
		Process pr = run.exec(cmd);
		try {
			pr.waitFor();
			BufferedReader buf = new BufferedReader(new InputStreamReader(pr
					.getInputStream()));

			String line;
			while (null != (line = buf.readLine())) {
				// pw.write(line);
				// pw.write("<br>");
			}
			pw.write("<a href=\"MyRssReader.apk\">Download</a>");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			pw.write(e.getMessage());
		}
		pw.write("</body></html>");
	}
}

web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

	<display-name>
		AppOnFLy
    </display-name>
	<description>
		Create your own android applications.
    </description>

	<servlet>
		<servlet-name>AppCreator</servlet-name>
		<servlet-class>AppCreator</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>AppCreator</servlet-name>
		<url-pattern>/go</url-pattern>
	</servlet-mapping>

</web-app>

Done.

whole workspace and look n’ feel was not good looking but it at least proved the concept.

Advertisements

6 comments

  1. Thanks for this wonderful tutorial.
    But can we create apps for different websites like this?ie suppose i have 100 websites and by creating a single app,can we change the logo,contents etc.
    Any help is appreciated.

    1. If you see it carefully, Servlet is just trying to change a file which is used in android application and then builds the application.

      So If you want to change logo, or any other content, you will have to get all the details from user and change the android project on fly. You will also have to ensure that files being replaced are properly used through your android code.

    1. It totally depends upon what and how you want to achieve with push notifications. If you can share some more information then probably it would be easy to answer your question.

      1. For eg: suppose i have a site which has many subdomains .Each of these subdomains have there own APK file .So if a user in subdomain 1(test.website.com) is downloaded its apk,then that apk should contain the logo of subdomain 1(test.website.com) and authentication is possible for registered users in that domain only.if any push is sent to that registered device,he should get that also.

        Likewise many subdomains and each have there own apk.Is it possible?

      2. Yes, all the features you mentioned can be configurable in your application. Your server should be smart enough to identify the subdomain and set the configurable parameters properly before building the app.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s