Monday, April 9, 2012

Handle defaulthttpclient redirects manually

By default DefaultHttpClient will handle redirect automatically. Sometimes we need to handle this manually, mainly if we need to set some parameters with every request.

We can do it by the following steps.
  1. Set HANDLE_REDIRECTS parameter value to false , that means it  defines redirects should be handled manually
HttpParams params = httpClient.getParams();
HttpClientParams.setRedirecting(params, false);
  1. Then after executing our request, if it is not HttpStatus.SC_OK or when response codes between 300 and 399 ( redirect responses )
    • Gets the values of the response header with the name as "Location"
    • the last value will be the new url where it need to redirect 
    • then execute the new request using the  redirect  url by setting the needed parameters
if (responseCode != HttpStatus.SC_OK) {
   Header[] headers = httpResponse.getHeaders("Location");
   if (headers != null && headers.length != 0) {
       String newUrl = headers[headers.length - 1].getValue();
     //Execute again with newUrl by setting the needed paramters

My complete doHttpRequest method

private void doHttpRequest(String url){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);

//getHttpParams is used to get the HttpParams 
//by setting all the required parameters

//Set HANDLE_REDIRECTS to false , 
//defines redirects should be handled manually
HttpParams params = httpClient.getParams();
HttpClientParams.setRedirecting(params, false);

HttpResponse httpResponse = 

int responseCode = 

if (responseCode != HttpStatus.SC_OK) {
Header[] headers = 
if (headers != null && headers.length != 0) {
  //Need to redirect 
 String newUrl = 
    headers[headers.length - 1].getValue();

//call the doHttpRequest 
//recursively with new url
} else {

Hope this help.
And please feel free to point out my mistakes... 

Saturday, November 5, 2011

Simple Content Provider for db operations

At last I concluded, it is ContentProvider which we need to use for database operation ..
Content Providers  
Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; 

  • Yes content providers are mainly aimed to share data amount different application
  • It can be also used inside our single application
  • There is no worries of closing and locking of db
  • It can be accessed from different activities,  services and Thread. Anywhere that have context object
  • I don't know is this a bad idea, I don't think so. Don't think it will reduce our data security. Our data can be accessed only with our AUTHORITY url
When a request is made via a ContentResolver the system inspects the authority of the given URI and passes the request to the content provider registered with the authority.
Now I'm trying to demonstrate the way how we can use ContentProvider in most simple way...

public class ContentProviderDb extends ContentProvider{
 OurDatabaseHelper dbHelper ;
 public static final String AUTHORITY = "ourContentProviderAuthorities";//specific for our our app, will be specified in maninfed 
 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
 public boolean onCreate() {
  dbHelper = new OurDatabaseHelper(getContext());
  return true;

 public int delete(Uri uri, String where, String[] args) {
  String table = getTableName(uri);
     SQLiteDatabase dataBase=dbHelper.getWritableDatabase();
     return dataBase.delete(table, where, args);

 public String getType(Uri arg0) {
  // TODO Auto-generated method stub
  return null;

 public Uri insert(Uri uri, ContentValues initialValues) {
  String table = getTableName(uri);
  SQLiteDatabase database = dbHelper.getWritableDatabase();
  long value = database.insert(table, null, initialValues);
  return Uri.withAppendedPath(CONTENT_URI, String.valueOf(value));

 public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
  String table =getTableName(uri);
  SQLiteDatabase database = dbHelper.getReadableDatabase();
  Cursor cursor =database.query(table,  projection, selection, selectionArgs, null, null, sortOrder);
     return cursor;

 public int update(Uri uri, ContentValues values, String whereClause,
      String[] whereArgs) {
  String table = getTableName(uri);
  SQLiteDatabase database = dbHelper.getWritableDatabase();  
  return database.update(table, values, whereClause, whereArgs);
 public static String getTableName(Uri uri){
  String value = uri.getPath();
  value = value.replace("/", "");//we need to remove '/'
  return value;

Here OurDatabaseHelper is the database helper class (extends SQLiteOpenHelper )
getTableName is used to get the specific Table name

Don't forget to add this in your manifest.xml file
<provider android:authorities="ourContentProviderAuthorities"
We specify our AUTHORITY as android:authorities

So the db operation will be like this:

Uri contentUri = Uri.withAppendedPath(ContentProviderDb.CONTENT_URI, "TableName");
Using these contentUri we are specifying our table where we need to perform our operation

We can perform db operation using our context


And if you are inside an activity class


For an example an insert operation will look like this

ContentValues initialValues = new ContentValues();
initialValues.put("Column", "value");
Uri contentUri = Uri.withAppendedPath(ContentProviderDb.CONTENT_URI, "TableName");
Uri resultUri = context.getContentResolver().insert(contentUri, initialValues);

Hope this help.
And please feel free to point out my mistakes... 

Sunday, September 25, 2011

Sending Multiple file attached email in android

Sending Multiple file attached email in android  ,

public boolean sendEmailWithMultipleAttachments(Context context,
	String[] emailTo, String[] emailCC, String[] emailBCC,
	String subject, String emailBody, List filePaths) throws ActivityNotFoundException {
final Intent emailIntent = 
            new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailTo);
emailIntent.putExtra(android.content.Intent.EXTRA_CC, emailCC);
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, emailBCC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);

if (filePaths != null) {
         // has to be an ArrayList
	ArrayList uris = new ArrayList();
	// convert from paths to Android friendly Parcelable Uri's
	for (String file : filePaths) {
		File fileIn = new File(file);
			if (fileIn.exists()) {
				Uri u = Uri.fromFile(fileIn);
                                         (Intent.EXTRA_STREAM, uris);
	context.startActivity(Intent.createChooser(emailIntent, "Sent mail"));
	return true;

filePaths is list, which store the local path of file which we need to attach

Get mime type of a file

In android to get mime type is very easy...

To find the mime type of a file, path

String extention =  path.substring(path.lastIndexOf(.) );
String mimeTypeMap =MimeTypeMap.getFileExtensionFromUrl(extention);
String mimeType = MimeTypeMap.getSingleton()

Wednesday, April 27, 2011

Create war file in grails

The war command will create a Web Application Archive (WAR) file which can be deployed on any Java EE compliant application server

grails war
grails test war
grails -Dgrails.env=foo war


grails [environment]* war [arguments]*
  • nojars - Packages the WAR with no jar files. Used for shared deployment
Fired Events:
  • StatusFinal - When the WAR file has been created
The war command will create a web application archive (WAR) file using the application name and version number. By default a war file deployable to the production environment will be created, but the environment can be specified using Grails standard convention
    grails test war
    grails dev war
    grails prod war
    grails war /foo/bar/mywar.war
    grails war --nojars

    Always better to call
    grails clean
    The clean command will delete all compiled resources from the current Grails application. Since Groovy is a compiled language, as with Java, this is sometimes useful to clear old instances of classes out and ensure correct compilation

    Refer war

    Saturday, April 16, 2011

    Using Fragment of Android Compatibility Package

    The Android 3.0 introduce a awesome feature Fragment.
    It really helpful when we design for tablet. You can refer The Android 3.0 Fragment API   for more details
    Here I'm going to explain how to use Fragment for lower version.
    Now android had released a static library that exposes the same Fragments API, so that applications compatible with Android 1.6 or later can use fragments to create tablet-compatible user interfaces.

    For this you need to do follow these steps
    • Add android-support-v4.jar to you project
      • You can found this jar at yoursdkpath/extras/android/compatibility/v4/
      • Copy this into your libs folder in the root of your project 
      • add this jar to your project build path
    • In order to use this you need to use FragmentActivity instead of Activity 
      • public class Home extends FragmentActivity
    • To get FragmentManager 
      • Need to use getSupportFragmentManager instead of getFragmentManage
    The complete code will look like this
    import android.os.Bundle;
    public class Home extends FragmentActivity{
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
      Fragment productDetails = new MyFragment();
      fragmentTransaction.replace(, productDetails);
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    public class MyFragment extends Fragment{
     public MyFragment(){
      Log.i("MYEXPENSETRACKER","MyFragment ()");
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      TextView t = new TextView(getActivity());
      t.setText("From Fragement");
      return t;
    The main.xml I used
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    Hope it helped you..:)

    Wednesday, April 13, 2011

    Go back to previous activity

    Its every common query how to go back to previous activity.
    Most of us will finish activity or keep track of the activity stack and all.
    But no need of this type of complex logic if you need just the same effect of user's back key press
    You can use the android onBackPressed() method explicitly .


    The default implementation simply finishes the current activity, but you can override this to do whatever you want.
    The android implicitly call this method when the activity has detected the user's press of the back key.
    So calling this explicitly will give the same effect