Android: A bare bones way to access App Engine

Post to Twitter

This is day five of my “five days of Android articles“. Today I’m going to modify yesterday’s example (Android: Using RestClient to communicate with Google App Engine) to access the Google App Engine project via a different way, a little more “bare bones” if you will. There is usually more than one way to do something so this article is to broaden the horizon bit. Personally I like the RestClient but today like I said is about a different approach.


We will use the same Google App Engine project that was created in the day four article. Make sure that project is up and running first. Next you can create a new Android project, I called mine “AndroidHTTPData”.

Once your Android project is created make sure to update the AndroidManifest.xml file to add the necessary permission since we are going to make a network (Internet HTTP) call:

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

If you don’t do that step you’ll probably see something like this:

Next on the list is to modify the “main.xml” file in the “res/layout” folder so we can dump the incoming data into a TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:id="@+id/outputTextView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:singleLine="false"
    />
</LinearLayout>

For the “AndroidHTTPData.java” activity the code looks like this:

package com.giantflyingsaucer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidHTTPData extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TextView textView = (TextView) findViewById(R.id.outputTextView);

		try
		{
			URL url = new URL("http://10.0.2.2:8888/listenforrestclient?message=" + URLEncoder.encode("Hello World","UTF-8"));
			URLConnection urlConnection = url.openConnection();
			HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;

			// Get the HTTP response code
			if(httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
			{
				InputStream inputStream = httpConnection.getInputStream();

				if(inputStream != null)
				{
					StringBuilder stringBuilder = new StringBuilder();
					String line;

					try
					{
						BufferedReader reader = new BufferedReader(
								new InputStreamReader(inputStream, "UTF-8"));

						while ((line = reader.readLine()) != null)
						{
							stringBuilder.append(line).append("\n");
						}
					}
					finally
					{
						inputStream.close();
					}
					textView.setText(stringBuilder.toString());
				}

			}

			if(httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
			{
				textView.setText("404 (not found)");
			}
		}
		catch (MalformedURLException urlEx)
		{
			textView.setText(urlEx.getMessage());
		}
		catch (IOException ioEx)
		{
			textView.setText(ioEx.getMessage());
		}
		catch (Exception ex)
		{
			textView.setText(ex.getMessage());
		}
	}
}

Running the Android project you should see this:

Post to Twitter

This entry was posted in Android, App Engine, Open Source. Bookmark the permalink.

2 Responses to Android: A bare bones way to access App Engine

  1. Pingback: Five days of Android articles coming next week! « Giant Flying Saucer

  2. RAM says:

    Good work, Thanks for the Article :)

Comments are closed.