Thursday, 20 September 2012

Random Key Generator

Sometimes we do need some sort of random key , say for example :

* Sending some unique password / key for one time authentication
* Generating some random user name
* Generating one time password
* validate email / mobile number

and so on ...

So why to bother every time writing some code for generating random number. Here is solution :

Download Random Generator Jar

Just include this jar file and use utility functions :

Example :

package testing;

import com.affiance.yim.gaurav.random.RandomKeyGenerator;

public class Testing {
public static void main(String[] s) {

Functions take a parameter, length of key you want to generate.

Enjoy Coding !

keywords for SEO  : java j2ee random secret unique key password alphanumeric alphabetic numeric length

Monday, 2 July 2012

Android Emulator Networking

Android Networking Issues :

connect form host to emulator , connect from emulator to host machine , ping host from emulator , ping emulator from host , server running in emulator and connect from through host, server running in host machine and connect from emulator

One of the simplest (but time eater) scenario I have faced is networking between your Android emulator and your Host machine.
Two problems I have faced are :

1) Connect Emulator to a Server running in host machine
2) Connect Host Machine to Server running in emulator

Out of these two, second one became real headache for me. But atlast solution I found was so simple.


Solution 1  : Connect emulator to server running in host machine :

For emullator , IP address of host machine is

So you can use this IP to connect to your host machine, for example:

Let us assume a web application ""sample" is running at port 8084 in your host machine.


and press enter, it will print your desired output.


Solution 2:

Connect Host machine to server running in emulator :

By default emulator IP is , but problem you will be facing is you can not connect using this IP and even
you can not ping this IP.

So solution I found was port redirection, very simple to do :

Assume one instance of emulator is running


i) open command prompt
ii) type " telnet localhost 5554 "  ans press enter  
iii)type " redir add tcp:fromPort:whichPort " and press enter

Step 3 here redirect your request, mean any request you will make using localhost:fromPort will deliver to emulator:toPort

iv) you can del redirection using command :

redir del tcp:fromPort

v) If you are working with udp, then use udp instead of tcp in command

Sunday, 1 July 2012

Java : Inheritance and Interfaces , A magic or what ?

In Java usually we see there are event listeners ( say OnClickListener, OnTouch, OnDataArrived  or any other ) , what we do is simply pass reference of our object to required method and it automatically calls
appropriate methods of our class based on events. Most of people think it as magic or do not think it at all. Now we are going to see after all how does it work !

All this thing is based on mainly two imoprtant properties of Java : Inheritance and Polymorphism.

To understand how the main stuff happens, you must have clear idea of these two properties. Let us dive into both one by one.

Inheritance :

Inheritance can simply be defined as inheriting properties of a class by another class. It is important to note we can inherit only  " public ,default and protected members " in same package and
only " public and protected members " in other packages. Take an example :

package first;               

public class Animal {
int defaultInt;
protected int protectedInt;
protected int age;

public void printAge(){
System.out.println("printing Animal's age");

public void animalSound(){
System.out.println("animal sound @@@@");


public class Dog extends Animal {

public void checkDefault( ) {

defaultInt = 9;                                                    // valid because Dog inhertis Animal and because of same package


public void checkProtected() {

protectedInt = 9;                                                            // valid because Dog inhertis Animal and because of same package


public void printAge(){
System.out.println("printing dog's age");

public void sound(){
System.out.println("bhow bhow");



package second;
import first.Animal;

public class Cat  extends  Animal {

public void checkDefault( ) {

defaultInt = 9;                                                    // not valid because of different package


public void checkProtected() {

protectedInt = 9;                                                            // valid because Cat  inhertis Animal


public void printAge(){
System.out.println("printing cat's age");

public void sound(){
System.out.println("meaau meaau");


Java allows us to define an object by :

ClassName object = new ClassName();


SuperClassName object = new SubClassName();                                    // valid , but we are restricting our object to call only methods and properties present in class SuperClassName


SubClassName object = new SuperClassName();                                    // invalid



Now coming to Polymorphism :

Simplest Definition will be : one name , multiple behavior.

Like we can have more than one same name function in a class, only diefference we  make is between their parameters.

These functions are called overloaded functions.

But there is one more term called overriding, it come into existance during inheritance. (I am not gonna explain overriding in detail here)

Simply Overriding mean having same signature of a function in both superclass and subclass with rules specified by Java.

As we saw above Java allows us :

SuperClassName object = new SubClassName();

considering above Animal example:

Animal animal = new Cat();

but now we can not call any cat specific function, since our reference is through Animal. We are restricted to call only functions present in Animal.

i.e :   animal.sound()                              // it is invalid

            animal.printAge()                       // is valid

            animal.animalSound()                // is valid

as expected
            animal.animalSound() will print "animal sound @@@@"

            but what do you expect on calling :

            animal.printAge();                      // printing animal's age   ??

but here polymorphism comes into picture

and output is :   printing cat's age

it mean in Java at compile time we can call only those methods which are present in Reference class, but at runtime if Actual class of which object was initialized ( using new )
overrides function, Java calls overridden function.

And that is how EventListener works in Java.

As you might have noticed almost all EventListeners are interfaces which incorporates some abstract functions that you might be interested if that event happens. So what actually happens is when we implement any
such interfacewe do implement all those functions. and I assume you are familiar with following term:

 ( YourClass impements OnClickListener )

button.setOnClickListener(this);              // assuming you are having any button in your class and want to know when its click event happens, this way you register your class and binds

Now have you ever wondered what magic happens at the other end (In Button class) , how button class knows about your object and passes proper events to your implemented function

Answer is polymorphism that we saw just in above example (Animal)

In Button class , It do nor need your object but it needs OnClickListener Object and since your class is implementing the same, so it becomes the required

Now in Button Class any whenever event happens, it just simply calls


and due to polymorphism, your class function gets called.

Any question , you can email me at :


Thursday, 12 April 2012

Java bypass proxy with code

Setting proxy in / with java code

As We normally use httpconenction class for url connection. Sometimes it can create problem, specially
when you are working in a System in which internet is blocked by any proxy. So you have to authenticate yourself by giving username and password .

Before your credentials you need to tell JVM which proxy you have and it is running on which port. This can be accomplished by following two lines.

   System.setProperty("http.proxyHost", PROXY);
            System.setProperty("http.proxyPort", PROXY_PORT);

Now we need to authenticate ourselves which can be done by using simple one line code given below :

            Authenticator.setDefault(new MyAuthenticator());

Here MyAuthenticator is authenticator class used to authenticate user, username and password, just
overide getPasswordAuthentication() function, i.e :

public class MyAuthenticator extends Authenticator {

    protected PasswordAuthentication getPasswordAuthentication() {

        return new PasswordAuthentication("UserName", "Password".toCharArray());

Copy and paste above Authenticator class in your project and replace username and password with yours.

                     Note:  Do all above task before making your URLConnection

Java Client http / https connection


If you are facing as simple problem as hitting an URL through java code and getting response back, you are at right place. It is damn simple. Just follow steps given below :

1 ) Include myclient.jar file in your project.  click here to download jar file

2) Make an object of ClientURLConnection.

3) Pass URL you want to hit.

3) Set method you want to send request (GET or POST)

4) Add parameters (key-value pair) , if any you want to send along with request.

5) Call method yourObject.getInputStream()

And it is done !! Now do not waste time here and complete your task :)