Webdeveloper on Android:Linking your app to your site

Boss wants an app for the company

So here I was wanting to write that Android app. Now I’ve played with Android before for a smaller app that makes shortners. But I wanted to do something bigger. So during a staff meeting evening I chatted on about android and iphone dev and me doing a shortner app for it.  My boss caught on and told me we should probably do something for the company on a mobile platform. So we did.

We settled on writing an app that will allow our customers , and potential new customers register a new domainname, and then either sign up or log in, provide payment details, and confirm the purchase after which the user is kept aprised of the purchase status until the registration is complete.

Also I’d like to point out that I am in no way a Java expert. I’m a webdeveloper, dealing mostly with PHP, HTML and JS in my dayjob. But I know of a few webdevelopers who dabble in the fringe science that is development for mobile platforms, maybe this view is interesting to some other likeminded nutjobs.

So now you know the backstory. This is what I ran into while writing my first major application:

Outdated blog posts and limited examples

One of the first things I figured out that there were several versions of the android platform around and that there were several blogposts with outdated information. So as a gentle reminder. Everything you see here is what I found out for android 1.6, I have no clue if anything I write here works for android 2.0 or further down the line. The android documentation could be more like php.net’s manual. For every method or class there is at least one example. That’d be nice for android too…

Splitting stuff up into activities
It is very important to split stuff up into different activities. This allows you to unclutter and keep stuff that belongs in the same activity together. Also this allows you to create a step-based process which allows the backbutton to finish.

HTTP requests

Another interesting one is doing http requests.There are a few options :

  1. the easy no fuss way : using “URL” aka java.net.URL:
    url = new URL("https://add.your.url/here");
    Object content = url.getContent();
    
    //two options: its an input stream or a string.
    if (content instanceof InputStream
    		|| content instanceof Reader)
    {
    	//open a buffer reader
    	BufferedReader in; 
    
    	if (content instanceof InputStream)
    	{
    		in = new BufferedReader(new InputStreamReader(
    				(InputStream) content));
    	}
    	else if (content instanceof BufferedReader)
    	{
    		in = (BufferedReader) content;
    	}
    	else
    	{
    		in = new BufferedReader((Reader) content);
    	}
    
    	//make an empty array of characters
    	char[] data = new char[10000]; 
    
    	//the position we are at
    	int pos = 0;  
    
    	while (true)
    	{
    		//If we're at the end of our array (ofcource you can read more just concat the character array to a string
    		if (pos == 10000)
    		{
    			break;
    		}
    
    		//Read the input
    		int ch = in.read();
    		if (ch == -1)
    		{
    			//whoops end of file
    			break;
    		}
    		//add the character to the array
    		data[pos] = (char) ch;
    		//update the position marker
    		pos++;
    	}
    
    	answer = new String(data, 0, pos);
    	//close your bufferreader
    	in.close();
    }
    else if (content instanceof String) {
    
    	answer = (String)content;
    }
    else
    {
    	answer="";
    }

    please note that this one can throw :
    IOException,MalformedURLException

  2. Using DefaultHttpClient aka org.apache.http.impl.client.DefaultHttpClient;
    private String requestPath(String path)
    {
    	String targetUrl =this.protocol+"://"+this.host+":"+this.port+path;
    	debugText("requesting","::"+targetUrl);
    	this.httpclient = new DefaultHttpClient();		
    
    	HttpPost httppost = new HttpPost(targetUrl);
    	List  nvps = new List ();
    	//add post vars
    	nvps.add(new BasicNameValuePair("varname", "valuegoeshere"));
    	try
    	{
    		httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    	}
    	catch (UnsupportedEncodingException e)
    	{
    		//skip it
    	}
    
    	//prepare a response
    	HttpResponse response;
    	try {
    
    		//do the actual request
    		response = this.httpclient.execute(httppost);
    
    		//you can get the headers with:
    		//response.getAllHeaders()
    
    		//you can get the cookies with:
    		List cookies = this.httpclient.getCookieStore().getCookies();
    
    		HttpEntity entity =  response.getEntity();
    
            //see if the result is null or not...
            if (entity != null)
            {
            	InputStream instream = entity.getContent();
    
                //using a method makes things so less cluttery
                String outputResult = this.convertStreamToString(instream);
                result =outputResult;
    
            }
    
    	} catch (ClientProtocolException e) {
    	} catch (IOException e) {
    	}
    	//make sure you close it down if you don't need it anymore
    	this.httpclient.getConnectionManager().shutdown();
    	return result;
    }
    
    private String convertStreamToString(InputStream is)
    {
    	BufferedReader reader = new BufferedReader(new InputStreamReader(is),1024*1024);
    	StringBuilder sb = new StringBuilder();
    
    	String line = null;
    	try {
    		while ((line = reader.readLine()) != null) {
    			sb.append(line + "\n");
    		}
    	} catch (IOException e) {
    		e.printStackTrace();
    	} finally {
    		try {
    			is.close();
    		} catch (IOException e) {
    			Log.e("exception while reading",e.toString());
    		}
    	}
    	try {
    		reader.close();
    	} catch (IOException e) {
    
    	}
    	return sb.toString();
    }

Worker threads
To prevent clogging up your UI thread (Which will result in a unresponsive application) you want to use a worker thread to do the http requests for you.
Here is an example using our earlier requestPath method

//lets pretend someone clicked a button and its listener called this method
public void clickOnTheRetrieveButton()
{
	//create a new thread
	Thread this.workerthread = new Thread(){
		public void run()
		{
			//get the response from the server using our httpclient method created earlier
			String response = (String) requestPath("/i/need/some/icecream.xml");

			//write back the response...
			cheesecake_activity.this.setResponse(response);

			//its also possible to use a messenger to update the data ... but i'll explain that in a later post

			//Notify the uithread that there is something to update
			cheesecake_activity.this.runOnUiThread(new Runnable(){
						public void run()
						{
							doSomethingWithResponse();
						}
			});

		}
	};

	//start the thread
	this.workerthread.start();
}
public void setResponse((String) response)
{
	this.response = response;
}
public void doSomethingWithResponse()
{
	Log.d("response is",":"+this.response);
}

Okay so now you have some example code on how to do http requests in android and not kill your application, Hope it helps…

I will be back next week posting about: How to test for network availability, Using a progressbar, Creating a dynamic form using a ListView.

Oh and the examples are copy pastes from the app I’m building, With some modifications, Which may result in not working code. If you find any boo-boo-s lemme know? Thx!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.