Thursday, November 6, 2014

Android : Usages & Issues of Toolbar (Material Compatibility) in Appcompat 21

Toolbar Widget

Toolbar is fully supported in AppCompat and has feature and API parity with the framework widget. In AppCompat,Toolbar is implemented in the android.support.v7.widget.Toolbar class. There are two ways to use Toolbar:
  • Use a Toolbar as an Action Bar when you want to use the existing Action Bar facilities (such as menu inflation and selection, ActionBarDrawerToggle, and so on) but want to have more control over its appearance.
  • Use a standalone Toolbar when you want to use the pattern in your app for situations that an Action Bar would not support; for example, showing multiple toolbars on the screen, spanning only part of the width, and so on.
Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:
  1. A navigation button
  2. A branded logo image
  3. A title and subtitle
  4. One or more custom views
  5. An action menu

Things to do remind while Implementing ToolBar

IllegalStateException (windowActionBar)

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

Solution:

add
 <item name="windowActionBar">false</item>  
in theme.

Apply Styles for Version above API level 14+


   <color name="holo_blue_light">#ff33b5e5</color>  
   <color name="green">#36A245</color>  
   <color name="ripple_material_light">#40000000</color>  
   <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">  
     <item name="windowActionBar">false</item>  
     <item name="colorPrimary">@color/holo_blue_light</item>  
     <item name="colorPrimaryDark">@color/green</item>  
     <item name="colorAccent">@color/ripple_material_light</item>  
   </style>  

Above 21+


   <style name="AppBaseTheme" parent="android:Theme.Material.Light">  
     <item name="windowActionBar">false</item>  
     <item name="colorPrimary">@color/ripple_material_light</item>  
     <item name="colorPrimaryDark">@color/green</item>  
     <item name="colorAccent">@color/holo_blue_light</item>  
   </style>  

Styling of ToolBar:

Styling of Toolbar is done differently to the standard action bar, and is set directly onto the view.

   <android.support.v7.widget.Toolbar  
     xmlns:app="http://schemas.android.com/apk/res-auto"  
     android:id="@+id/my_awesome_toolbar"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:background="?attr/colorPrimary"  
     android:minHeight="?attr/actionBarSize"  
     app:theme="@style/ThemeOverlay.AppCompat.Light" />  

ThemeOverlay.AppCompat.Light : Changes the Toolbar title text color to Black.
ThemeOverlay.AppCompat.Dark.ActionBar : Changes the Toolbar title text color to White.


Usage of inflateMenu in Toolbar:

If you're using setSupportActionBar(toolbar); then inflateMenu of toolbar cannot be used, because Toolbar is acting like Actionbar. So, all the menu related callbacks are default.

The only time you need to call toolbar.inflateMenu() is when you are using the Toolbar as a standalone widget. In this case you need to manually populate the Toolbar with content/actions. For instance, if you want it to display actions, you need to inflate a menu into it:

 Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);  
 // Set an OnMenuItemClickListener to handle menu item clicks  
 toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {  
 @Override  
 public boolean onMenuItemClick(MenuItem item) {  
      switch (item.getItemId()) {  
      case R.id.searchItem:  
           Toast.makeText(getApplicationContext(), "Search", Toast.LENGTH_SHORT).show();  
           break;  
      case R.id.filterItem:  
           Toast.makeText(getApplicationContext(), "Filter", Toast.LENGTH_SHORT).show();  
           break;  
      }  
      return true;  
      }  
 });  
 // Inflate a menu to be displayed in the toolbar  
 toolbar.inflateMenu(R.menu.toolbar_menu);  
 
res/xml/toolbar_menu.xml  
 <?xml version="1.0" encoding="utf-8"?>  
 <menu xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto" >  
   <item  
     android:id="@+id/searchItem"  
     android:icon="@android:drawable/ic_menu_search"  
     android:title="Search"  
     app:showAsAction="always"/>  
   <item  
     android:id="@+id/filterItem"  
     android:icon="@android:drawable/ic_menu_info_details"  
     android:title="Info"  
     app:showAsAction="always"/>  
 </menu>  


Navigations in Toolbar:

Here we can see how toolbar navigation works and this is similar to the action of setDisplayHomeAsUpEnabled(true) in the actionbar.
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setTitle("Second Activity");
toolbar.setNavigationIcon(R.drawable.ic_up);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
           NavUtils.navigateUpTo(SecondActivity.this, IntentCompat.makeMainActivity(new ComponentName(SecondActivity.this, SecondActivity.class)));
           }
});


Customize Toolbar:

The customization of the toolbar here is to center align the title text and it is as follows.

<android.support.v7.widget.Toolbar  
     xmlns:app="http://schemas.android.com/apk/res-auto"  
     android:id="@+id/my_awesome_toolbar"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:background="?attr/colorPrimary"  
     android:minHeight="?attr/actionBarSize"  
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >  
     <TextView  
       android:id="@+id/toolbar_title"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_gravity="center"  
       android:singleLine="true"  
       android:text="Toolbar Title"  
       android:textColor="@android:color/white"  
       android:textSize="18sp"  
       android:textStyle="bold" />  
</android.support.v7.widget.Toolbar>  


Source Code
You can download the source code by clicking here: ToolBarMaterialComp Source Code.  This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu. It works in all API levels above 2.3.

As always, thanks for reading :) 
Don't forget to +1 this blog and share this post on Google+ if you found it interesting!

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments & feedback's are welcome.

Wednesday, September 24, 2014

Android: Material Theme in Kitkat

Material Theme look and feel can be easily achieve in KitKat because of the new features Translucent UI in KitKat, where you have the ability to sit behind status and navigation bars

We can getting material theme look and feel in 2 ways.

1. We can write our own styles.xml and modify according to our needs.
2. Import SystemBarTint library and apply changes as per need.

First Approach using styles.xml:

We can build our own theme by setting with Translucent Theme in xml

If you are seeking more control over translucent decor, consider using a custom theme that extends something other than *.TranslucentDecor and use the following style properties in your theme:

 <item name="android:windowTranslucentStatus">true</item>  
 <item name="android:windowTranslucentNavigation">true</item>  

If your content does not need to be full bleed you can structure your layouts in such a way that the content will reside in an area of the screen that is not blocked by the status or navigation bar. To do this you would set android:fitsSystemWindows="true" on the content layout/view in your layout xml or in style.xml as
  <item name="android:fitsSystemWindows">true</item>.  
.
Add Some Color to the status bar, just set the windowBackground attribute in my theme.
 <item name="android:windowBackground">@color/window_bg</item>  

Second Approach Using SystemBarTine:

This library jgilfelt-SystemBarTint offers a simple way to create a background "tint" for the system bars. Internally even this library is depended on the Translucent  theme.


Source Code
You can download the source code by clicking here: MaterialThemeLookInKitKat.  This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu. This example is built using 2nd approach.

Output:



Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments.

Tuesday, September 23, 2014

Solution: Stuck @ installing apps from Google Play Store

Problem

If you are stuck at some percentage while installing apps from Google Play Store.

Reason

The reason behind this is Google Play Store transfers data in background to update itself. Without completing the background data transfer, Play Store won’t start downloading apps.
It will be showing progress bar/loading bar will be running and showing percentage but the download won’t start until your Google services job is finished.

Solution

Wait a few minutes until the Google Play Store is completely upgraded.

Even after, If the problem persists, then follow the steps below.

  • Go the play store and start installing application.
  • Now, minimize play Store and go to Settings  Apps  RUNNING
  • Find the Google Play services. Tap and stop all the Google Play Services.
  • Now Go back to play store again and your download will start and finishes... 

Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? feedback are welcome.

Friday, September 12, 2014

Android: Image loading libraries Picasso vs Glide

This post is about image loading libraries, which could help us in avoiding complex codes.

Downloading of images, caching and reusing of images are know part of all the application and it became part of developers life. While doing this process, We need to consider few things...

  • Loading process should be asynchronously.
  • Image should display when it is loaded successfully, In case if it is not downloaded then placing some place holders.
  • Caching Image rather than downloading every time.

There are more libraries in the market which helps us in avoiding to do all these exercises and here I am going to discuss about Picasso and Glide. Where this two libraries looks similar in the way of implementation and Its easy to integrate in the middle of the application, since its a just a one line code to load image.

There are lot of features in the two libraries and few are listed below, where I can compare them easily.

Wednesday, September 10, 2014

Android Wear : Solution for "You cannot combine swipe dismissal and the action bar"

While running my first application in android wear in eclipse. I have faced with the exception.

Exception:
android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.access$800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5026)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
Workaround Solution:
There should be some SDK issues where it is behaving weirdly.
So, I have tried by removing android:targetSdkVersion and change the android:minSdkVersion as shown below it worked out for me.

Change to  
<uses-sdk android:minSdkVersion="8" /> 

from 
<uses-sdk android:minSdkVersion="20" android:targetSdkVersion="20" />



Monday, September 8, 2014

Glide, an Android media loading library...

Recently I have came across new image loader library Glide from iosched Git Code

Glide

Glide is a fast and efficient open source media management framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

Glide's primary focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide is also effective for almost any case where you need to fetch, resize, and display a remote image.

Implementation / Usage

When we see the implementation of Glide. It looks similar to the Picasso.

Let Looks the implementation of Picasso and Glide.

Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Glide.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Using the Picasso or Glide are easy and straight forward.

Advantage of Glide here is... Inbuilt it supports caching...

Let us assume, I am having two ImageView where one is loaded with Picasso and othere with Glide as shown below.

Saturday, September 6, 2014

Android L: UI Widgets : RecyclerView

RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.

RecyclerView is easy to use, because it provides:
  • A layout manager for positioning items
  • Default animations for common item operations
You also have the flexibility to define custom layout managers and animations for this widget.

To use the RecyclerView widget, we have to specify an adapter and a layout manager.

LayoutManager

The LayoutManager is probably the most interesting part of the RecyclerView. This class is responsible for the layout of all child views. There is one default implementation available: LinearLayoutManager which you can use for vertical as well as horizontal lists. To create a custom layout, you extend the RecyclerView.LayoutManager class.

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);


// improve performance if you know that changes in content
// do not change the size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

Animations

Animations for adding and removing items are enabled by default in RecyclerView.
use the RecyclerView.setItemAnimator method for animations, to customize just extend the RecyclerView.ItemAnimator class

Android provide DefaultItemAnimator and we can customize according to our needs.
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

Adapter:

To create an adapter, you extend the RecyclerView.Adapter class.
The RecyclerView.Adapter class contains few notifyItemXXXX()methods. Which help us in modifying the View.

Two methods used in the example are insert and remove and they are
notifyItemInserted(position);
notifyItemRemoved(position);

mRecyclerView.setAdapter(mAdapter);

// insert or remove item from the view. This methods are in the adapter class.

public void addToList(String name) {
int position = 0;
if (mDataset.size() > 1) {
  position = new Random().nextInt(mDataset.size() - 1);
}
mDataset.add(position, name);
notifyItemInserted(position);
}

public void removeItemFromList(String name) {
int position = mDataset.indexOf(name);
if (position != -1) {
  mDataset.remove(position);
  notifyItemRemoved(position);
}
}



Source Code
You can download the source code by clicking here: Sample RecyclerView .  This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu.

Screenshot:
Initial screen with four items.

Added two rows to the recyclerView
Removed one item which is added from RecyclerView
Thanks for reading :) 
Whether this post is helpful?


Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments and feedback are welcome

Friday, September 5, 2014

Android: RecyclerView cannot be resolved to a type

RecyclerView cannot be resolved to a type? How to resolve it?

We need recyclerview-v7 Support Library to solve the issue. but how to get? letc check...

Check whether Recyclerview is existing in the SDK or not because it is a part of the support library.

If you're not find the folder “recyclerview-v7” in the following sdk directory "..\sdk\extras\android\m2repository\com\android\support" then follow the steps otherwise follow from step 3.

Steps to achieve:
  1. Select the latest Android SDK Tools, Platform-tools, and Build-tools.
  2. Select latest Android Support Library & Android Support Repository.
  3. After updating Android Support Repository from SDK Manager go to..\sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0-rc1
  4. Unzip recyclerview-v7-21.0.0-rc1.aar
  5. After unzipping recyclerview-v7-21.0.0-rc1.aar you will get classes.jar
  6. rename classes.jar to recyclerView.jar for feature reference and add to your Android app under /lib folder
Overview:
RecyclerView updates and replaces ListView. It's claimed to be easier to use and feature performance improvements in dynamic views.

RecyclerView is easy to use, because it provides:
  • A layout manager for positioning items
  • Default animations for common item operations

The most important classes of the RecyclerView API.

Class
Usage
Adapter
The Adapter is also responsible for making a View for each item in the data set.
LayoutManager
A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user.
ItemAnimator
Animations for adding and removing items are enabled by default in RecyclerView



Thanks for reading :)
Any suggestions can be added in comments. feedback are welcome.

Thursday, September 4, 2014

Android: Notification in L

Notification in Android L:

In L, notifications receive an important structural visual and functional update:
  • Visual changes to notifications as part of material design
  • Notifications are now available on the device lock screen, yet sensitive content can still be hidden behind it
  • A new presentation format called Heads-up for receiving high priority notifications while using the device
  • Cloud-synced notifications - act on a notification on your Android tablet and it is also dismissed on your phone.
  • And starting now (in Android 4.4W, API Level 20, the platform release for Android Wear), your notifications will bridge to Android Wear devices. You can extend the functionality of notifications on Wear in two different ways. First, you can add speech input and canned responses to Actions on Wear, allowing users to complete tasks from their wrists. Second, you can write Wear apps that hook into your notifications to go even further in terms of creating interactive experiences for users.

For more information of notifications ref android notifications.

Sample Code for showing notification

      /**  
       * Send a sample notification using the NotificationCompat API.  
       */  
      public void sendNotification(View view) {  
           Intent intent = new Intent(  
                     Intent.ACTION_VIEW,  
                     Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));  
           PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  
                     intent, 0);  
           NotificationCompat.Builder builder = new NotificationCompat.Builder(  
                     this);  
           builder.setSmallIcon(R.drawable.ic_stat_notification);  
           // Set the intent that will fire when the user taps the notification.  
           builder.setContentIntent(pendingIntent);  
           builder.setAutoCancel(true);  
           builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),  
                     R.drawable.ic_launcher));  
           builder.setContentTitle("BasicNotifications Sample");  
           builder.setContentText("Time to learn about notifications!");  
           builder.setSubText("Tap to view documentation about notifications.");  
           NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
           notificationManager.notify(NOTIFICATION_ID, builder.build());  
      }  

Output:



In first pic, we can see the notification received in the status bar.
On drag of screen from top. We can see, how the basic notification appears.


Wednesday, September 3, 2014

Quick glance of Android L features

Android L

Android L is a next version of kitkat, Coming with new designs, features and updates...

Android L is Faster, better looking and more efficient.

Android L is going to change the way it looks with material design- These projects extend Android to the TV (Android TV), to the car (Android Auto) and to wearables (Android Wear), among others.

See the API overview for more information on the rest of the new and updated features. Lets look a quick view of all the API's.

Wednesday, August 13, 2014

Solution/Precaution to avoid android.os.TransactionTooLargeException

Exception: :mad:

 java.lang.RuntimeException: android.os.TransactionTooLargeException  
 at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:568)  

Reason: ;))

The Binder transaction failed because it was too large.
During a remote procedure call, the arguments and the return value of the call are transferred as Parcel objects stored in the Binder transaction buffer. If the arguments or the return value are too large to fit in the transaction buffer, then the call will fail and TransactionTooLargeException will be thrown.

Possibility Cases: :-w 

There are two possible outcomes when a remote procedure call throws TransactionTooLargeException.

  • Either the client was unable to send its request to the service (most likely if the arguments were too large to fit in the transaction buffer).
  • The service was unable to send its response back to the client (most likely if the return value was too large to fit in the transaction buffer).
It is not possible to tell which of these outcomes actually occurred. The client should assume that a partial failure occurred.

Assumption: :-?

While adding account to AccountManager. Adds an account directly to the AccountManager with the help of service call.
When there is a huge amount of data getting exchanged between a service and an application, we will get android.os.TransactionTooLargeException.

Solution/Precaution to avoid android.os.TransactionTooLargeException *-:)

We don't have direct solution but we can take precaution to avoid exception.
  1. Try to keep all transactions relatively small.
  2. Do not exchange huge data (roughly not greater than 512KB) between services and application because the Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. 


Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments and feedback are welcome...

Wednesday, August 6, 2014

Solution for ClientProtocolException Caused by CircularRedirectException.

Exception occurred while hitting URL:X(:((

 org.apache.http.client.ClientProtocolException   
 07-28 04:26:53.349: W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)  
 07-28 04:26:53.349: W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)  
 07-28 04:26:53.349: W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:78)  
 07-28 04:26:53.349: W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:102)  
 07-28 04:26:53.349: W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:58)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.FutureTask.run(FutureTask.java:234)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)  
 07-28 04:26:53.349: W/System.err(1276): at java.lang.Thread.run(Thread.java:841)  
 07-28 04:26:53.349: W/System.err(1276): Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'redirectURI'  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:173)  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:923)  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:475)  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)  


Details / Information about the Exception: :-?


ProtocolException : Signals that an HTTP protocol violation has occurred. For example a malformed status line or headers, a missing message body, etc.
When RedirectHandler determines the location request is expected to be redirected to given the response from the target server and the current request execution context.

public static final String ALLOW_CIRCULAR_REDIRECTS = "http.protocol.allow-circular-redirects";
Defines whether circular redirects (redirects to the same location) should be allowed. The HTTP spec is not sufficiently clear whether circular redirects are permitted, therefore optionally they can be enabled

When the redirection and the parameter of "http.protocol.allow-circular-redirects" is false it works for the first time and from the second time it throws  CircularRedirectException("Circular redirect to '" + redirectURI + "'")

We may get doubt, why first time we aren't getting the exception and allowed but not the second time? *confused*

Let look getLocationURI method of DefaultRedirectHandler.java class, we can found the code snippet as follows.


 if (redirectLocations.contains(redirectURI)) {  
      throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");  
 } else {  
      redirectLocations.add(redirectURI);  
 }  

When first time we hit, the redirectLocations object is null and it will initializes
 RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);  
 if (redirectLocations == null) {  
      redirectLocations = new RedirectLocations();  
      context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);  
 }  

So, the redirectURI wont be available in the redirectLocations object. When we hit the second time, the redirectURI is existing in the redirectLocations object. So, the apache throwing CircularRedirectException.

Solutions: :-bd

Here we have 2 solutions, either one of them we can use.
1. getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

2. extend DefaultRedirectHandler and modify getLocationURI method.




Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments / feedback's are welcome. :-h