Friday, 11 May 2012

Session maintenance using cookies.

Session maintenance using cookies.

In this post i am going to explain how to retrieve and save cookies from the HTTP Post request and send it further with HTTP Get requests.

Retrieving and storing cookies:

Suppose you have a login web service and after successfully authenticating the server provides you with a cookie then following snippet can be used to retrieve and store the cookies.

DefaultHttpClient httpclient = new DefaultHttpClient();HttpPost httppost = new HttpPost(URL);httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  nameValuePairs.add(new BasicNameValuePair("username", YOUR_USERNAME));nameValuePairs.add(new BasicNameValuePair("password",YOUR_PASSWORD);httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  HttpResponse httpResponse = null;httpResponse = httpclient.execute(httppost);
           List<Cookie> cookies = httpclient.getCookieStore().getCookies(); //  Storing cookies
cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None");//No cookie found } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); // Cookie found 
} }

Now you have retrieved cookies and stored them in the Array List.
Cookie has various parts as follows:
1.Session ID.
2.Cookie name.
3.Domain etc...

Save the required fields from in preferences of cookie store so that you can resend these cookies to get requests to make other services call.

Sending cookies to make other web services calls:

Now you can send these cookies along with the get requests to make other web services calls.
Following snippet shows how to do it.

HttpClient httpclient = new DefaultHttpClient();HttpGet httpget = new HttpGet(url);HttpResponse response;COOKIE_VALUE=PreferenceConnector.readString(activity, PreferenceConnector.COOKIE_SESSION_ID, null); //I had stored the cookies in Prefrences and was retrieving the cookie session ID from there. 
httpget.setHeader("Cookie",  "JSESSIONID="+COOKIE_VALUE); //Here i am sending the Cookie session ID try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); RESULT_FINAL = convertStreamToString(instream); instream.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }


Cheers...... 

Sunday, 4 December 2011

Android Saved Instance State Bundle

Hi,
Normally when the device is tilted(i.e. screen orientation is changed) the activity is restarted.Whole activity is destroyed and recreated.So when activity is destroyed ,we need to save the state of activity.This can be done using the onSaveInstanceState and onRestoreInstanceState.


public class SavedInstanceActivity extends Activity {
TextView main_text;
Button update_button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        main_text=(TextView)findViewById(R.id.main_text);
        update_button=(Button)findViewById(R.id.press_saved);
        update_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
main_text.setText("SAVED INSTANCE STATE");
}
});
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("SAVED", main_text.getText().toString());
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    main_text.setText(savedInstanceState.getString("SAVED"));
    }
}

Like as you see in the above example if we had not used the onSavedInstanceState() then whole activty would have been recreated and textview would have been blank again.

Click to download the full source code: SOURCE CODE

Thursday, 24 November 2011

Steve Wozniak(Apple Co-Founder) with his Galaxy Nexus and a ice cream sandwich T-shirt to go with it.

Heck of a Strange thing happened in Google's campus in Mount View when Steve Wozniak the co founder of apple became the first person in US to get the Samsung Galaxy Nexus.

This all happened when "Woz" tweeted that he was looking for the Samsung galaxy nexus ,but unfortunately it was not available in US.So "Woz" was invited by the Google's campus in mount view  where he was presented the Brand new Samsung galaxy Nexus and a ice cream sandwich T-shirt.

gsmarena 001 Steve Wozniak gets a Galaxy Nexus from Google, and an Ice Cream Sandwich T Shirt to go with it




;-)....

SOURCE

Wednesday, 2 November 2011

Android Views Animation Tutorial




In this section i will try to explain the 4 types of animation on views which can overall change the impression of the views.

The attached program demonstrates the available types of animations which are:
Alpha - Causes a fading in or fading out effect.
Scale - The view's size smoothly changes.
Rotate - The view rotates.
Translate - The view moves to a different position.


You can individually use these animations or you can also combine these animations into one. Combining of animations is done with help of animation set.First i will explain how to use these animations individually.

ALPHA ANIMATION: Alpha animation is used in fading in and fading out of views. 

<alpha 
android:fromAlpha="0.0" 
android:toAlpha="0.9"
android:duration="3000" />

SCALE ANIMATION: Scale animation is used for changing the size of view.


<scale 
android:fromXScale="1.0" 
android:fromYScale="1.0"
android:toXScale="2.0" 
android:toYScale="2.0" 
android:duration="3000" />

ROTATE ANIMATION: Rotate animation is used for rotating a view around a particular axis.


<rotate 
android:fromDegrees="359" 
android:toDegrees="0"
android:duration="3000" 
android:pivotX="50%" 
android:pivotY="50%" />

TRANSLATE ANIMATION: Translate animation is used to change the position of a particular view.

<translate 
android:fromXDelta="300%" 
android:toXDelta="0%" 
android:fromYDelta="0%"
android:toYDelta="0%" 
android:duration="3000" 
android:zAdjustment="bottom" />

You need to keep these XML files in anim folder in res folder.
And finally to use this application in your Activity you need to use the following piece of code:




final Animation anim=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);// where animation is the name of the anim file which contains any of the above mentioned animation.




Download free source code here.

Tuesday, 1 November 2011

(Nokia choosing WP7 over Android)

                 As my title says i am going to talk about the thing which is in every one's mind i.e. why Nokia chose WP7 over Android although knowing the success of Android in the mobile market.So i m going to compare some of the features of Android and WP7.

                 During the Q&A during Nokia's Capital Market Day, Elop said that Android did not allow them to differentiate enough. This seems weird, since Android is considerably more open and free than Windows Phone 7. However, I think what he meant is that Nokia would not have a say in the actual development direction of Android. And this is where Microsoft comes in.

Nokia knew that it was too late to join the Android party—Android had become a crowded space. Creating a standout Android phone would not only be difficult given the experience of other companies but because it could get lost in the maul of Android phones available. Even Android phone makers would admit that it's hard to stay at the top of the Android power rankings—hell, doesn't it seem like the "best Android phone" only has a lifespan of a month before it gets dethroned? Nokia saw Google eventually getting all the profits as hardware gets commoditized.

And maybe Windows Phone 7 will have those same hardware "issues" as Android but Nokia felt it could differentiate its handsets by dropping in Nokia specific features like their Navteq location-based services. That, they felt, would give them a leg up.

But none of those arguments seem to matter as much as this one: Nokia didn't want to lose the smartphone wars without giving Google and Apple an honest fight. Choosing Android would mean that they'd become a bit player in a two-horse race (Apple vs Google). Choosing Windows Phone means they're hoping and fighting to add another horse. It might be too late and maybe Nokia had to sell their soul, who knows, but I respect any one who goes down swinging.

Monday, 31 October 2011

Lego robot beats human world record for solving the Rubik’s Cube with Android





cubestormer

Solving the Rubik’s Cube puzzle popularized in the 1980s is an exercise in futility for many of us, but to a world class “speedcuber” it’s a challenge of mere moments. The currentofficial human record for the Rubik’s cube 3×3 puzzle is just 5.66 seconds. I know a robot that did it in 5.35 seconds. Take, that humanity! (Wait a second…) The puzzle solving bot is called CubeStomer II and you can watch its record breaking run in the video below. Made from Lego, powered by an Android smart phone, and created by two mad geniuses, CubeStormer II has a pedigree that sounds like it came from a children’s cartoon. That’s fitting, because I think the Lego NXT robot is a sure sign that humanity has gone meta. Why waste time on solving trivial puzzles when you can build a robot to waste time solving trivial puzzles for you instead?
CubeStormer II is controlled by four Lego NXT ‘bricks’ that communicate via Bluetooth with a Samsung Galaxy SII smart phone. A special app on the phone takes a picture of the cube, solves the puzzle virtually, and then relays the solution to the Lego robot. From click to finish the whole process takes just seconds. You have to see it to believe it:

New Rejoice for Mob.Application Developer.

Hi friends yet another IDE is out for mobile application developers(I-Phone, Blackberry,Android)
Adobe Flash Builder 4.5
Now it seems as if  the java developers have some competition.
Language used to write the application  markup language that Flash Builder uses to design application interfaces.
When you create a Flex mobile project, Flash Builder generates the following files for the project (where ProjectName is the name you give your project in the first step of the wizard):
  • ProjectName.mxml: This is the application file for the project. Typically, you do not add content to the default application file directly.
  • ProjectNameHomeView.mxml: This file represents the initial screen (or view) of the project. Flash Builder places the file in a package called "views."
Now lets see if it brings the boom for the Flex developers...or bane for the Android java developers.

For complete and full documentation for Adobe flash builder check out this link.
http://www.adobe.com/devnet/flash-builder/articles/hello-world.html