Wednesday, March 28, 2012

Asynchronous Web Service Call in Android Native Application


Recently I was exploring ways of calling a web service asynchronously in Android. I was looking for an option that would work along the lines of XMLHttpRequest AJAX request in Javascript.
Following is one way of doing it
To execute an asynchronous task, Android OS itself provides a class called AsyncTask<Params, Progress, Result> which enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Having said that we can extend the class like  below
public class RemoteCallTask extends AsyncTask<String, Void, ArrayList<String>>
{
RemoteCallListener listener = null;
private final HttpClient httpClient = new DefaultHttpClient();
private String responseText;
public RemoteCallTask(RemoteCallListener listener)
{
this.listener = listener;
}
@Override
protected ArrayList<String> doInBackground(String… urls)
{
ArrayList<String> arrayList;
// Call the webservice and assume the response is JSONArray
// Construct the array list and return
return arrayList;
}
protected void onPostExecute(final ArrayList<String> arrayList)
{
// The returned arraylist in doInBackground() will be sent as an input
parameter here
// Now call the listener’s onRemoteCallComplete method and set the value.
listener.onRemoteCallComplete(arrayList);
}
}
The RemoteCallListener referred in the above code is an interface which looks like below
public interface RemoteCallListener
{
public void onRemoteCallComplete(ArrayList<String> arrayList);
}
Now wherever you need to call the web service do the following things
1) Implement the RemoteCallListener interface.
2) Provide an implementation for onRemoteCallComplete() method.
3) Call the AsyncTask like below
new RemoteCallTask(this).execute(url);
Now the webservice response will be received in onRemoteCallComplete() method.
That’s all.  Please let me know if there is any other better method to achieve this.

Creating Web Application in Android


The following article talks about creating an offline web application in Android using android.webkit package.

Android – application debug logging


Following content is referenced from
http://www.winksaville.com/blog/programming/debug-logging-in-android/
Here are a couple of places that got me started. First the standard java logging mechanism doesn’t work. In addition System.out.print doesn’t work. You need to use the android.util.log.
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
@StateMachine
public class test extends Activity
{
/** Called with the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
Log.v(TAG, “onCreate: E”);
setContentView(R.layout.main);
Log.v(TAG, “onCreate: X”);
}
private static final String TAG = “test”;
}
The Log class supports various levels (VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT) the low-level routine is Log.println(int priority, String tag, String msg). But typically the v, d, i, w, e and a convince routines would be used. So that’s half the game, the other half is to be able to see the output. For that you use “logcat”. In Eclipse if you goto menu “Window/Show View/other/Android/logcat” or if your in the Debug perspective it will be in “Window/Show View/logcat”. Apparently, its not very reliable in Eclipse and executing:
adb logcat
Allows you to see it from a command line, but in Eclipse the log also includes a Date stamp which makes it nice. I was able to get the Eclipse/logcat going by closing than reopening. Of course you mileage may very:) If you look at the links above it appears you can use ddms, but I haven’t used that yet.

Enabling Internet Connection Through Proxy in Android in Windows


Steps for Android 1.5
Following content is referenced from
In the earlier versions of Android emulators (up to version 1.1r2), you were required to make an entry in the system table of“com.android.provider.setting.db” database and/or start the emulator with –http-proxy switch supplying it the IP address and port number of your proxy.
These methods however, have become obsolete and don’t work with SDKv1.5.
Step 1: On emulator, go to:
Home->Menu->Settings->WirelessControls->MobileNetworks->Access Point Names->T-mobile US->set Proxy IP and Port#
Step 2: Now the next time you go to a web address, the browser will prompt for your user-id and password. Enter your credentials and you should be good to go.
Steps for Android 2.2
1) Create an emulator with name say android_2.2_emulator using AVD manager in eclipse.
2) Create a batch file (.bat) with following command and a keep a handy shortcut.
emulator -avd “android_2.2_emulator” -http-proxy “your_proxy_url”:”port”
3) Run the batch file before starting the eclipse IDE, so that the emulator session will have internet connection.
That’s it.

Android Forums and Blogs --- Useful

Android -- Installation


This guide is very quick start installation guide to get you up and running with the Google Android Software Development Kit (SDK).
First you’ll need to download the Android SDK source files: (http://developer.android.com/sdk/index.html)
System Requirements
1) Operating systems
  • Windows XP or Vista
  • Mac OS X 10.4.8 or later (x86 only)
  • Linux (tested on Linux Ubuntu Dapper Drake)
2) Development environment
a) Eclipse
b) Other development environments or IDEs
  • JDK 5 or JDK 6 (JRE alone is not sufficient)
Not compatible with Gnu Compiler for Java (gcj)
  • Apache Ant 1.6.5 or later for Linux and Mac, 1.7 or later for Windows.
Installing The Android SDK
1) Download the Android SDK pack .zip archive.
2) extract the zipped files in suitable location.
Please note:
This installation location will be referred to as $SDK_ROOT.
Alternatively you can add /tools to your root path which will prevent the need to specify the full path to the tools directory along with enabling you to run Android Debug Bridge adb) along with other command line tools.
Linux
  • Edit the ~/.bash_profile or ~/.bashrc files looking for a line that sets the PATH variable. Add the full path location to your $SDK_ROOT/tools location for thePATH variable.
  • If no PATH line exists you can add the line by typing the following:
export PATH=${PATH}:<path to your $SDK_ROOT/tools>
Mac OSX
  • In the home directory locate the .bash_profile and locating the PATH variable add the location to your $SDK_ROOT/tools folder.
Windows XP/Vista
  • My Computer icon -> Right Click -> Properties -> AdvancedTab -> EnvironmentVariable.
  • In the new dialog box double-click on Path (located under System Variables) and type in the full path location to the tools directory.
Android Eclipse Plugin (ADT)
If you choose to use the Eclipse IDE as your Android development environment you will have the opportunity to install and run a plug-in called Android Development Tools. ADT comes with a variety of powerful tools and extensions that will make creating, running and debugging your Android applications much easier and faster.
In order to download and install ADT you will first need to configure an Eclipse remote update, this can achieved via the following steps:
1. Start Eclipse, then select Help > Software Updates > Find and Install….In the dialog that appears, select Search for new features to install and press Next.
2. Press New Remote Site.
3. In the resulting dialog box, enter a name for the remote site (e.g. Android Plugin) and enter this as its URL: https://dl-ssl.google.com/android/eclipse/.
Press OK.
4. You should now see the new site added to the search list (and checked).Press Finish.
5. In the subsequent Search Results dialog box, select the checkbox for Android Plugin > Eclipse Integration > Android Development Tools and press Next.
6. Read the license agreement and then select Accept terms of the license agreement, if appropriate. Press Next. Press Finish.
7. The ADT plugin is not signed; you can accept the installation anyway by pressing Install All.
8.Restart Eclipse.
After restart, update your Eclipse preferences to point to the SDK root directory
9. ($SDK_ROOT):
Select Window > Preferences… to open the Preferences panel.
(Mac OS X: Eclipse > Preferences)
Select Android from the left panel.
For the SDK Location in the main panel, press Browse... and find the SDK root directory.
10. Press Apply, then OK.