Thursday 30 May 2013

Get Location Based on IP Address

There are many vendor available in market that provides REST service to get Location based on IP Address. In our example we have used IPInfoDB.

Step 1) Create an account with IPInfoDB: http://ipinfodb.com/register.php



Once account is created, you will receive an Account Activation link from IPInfoDB:

Activate the account.

Step 2) Login into IPInfoDB and under Overview Tab, copy the API Key. Using this API Key we will    call IPInfoDB Rest API to get Location.

Step 3) Java Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class IPInfoDB {
 public static void main(String[] args) {
  new IPInfoDB().getClientLatLng("IP_ADDRESS");
 }
 
 public void getClientLatLng(String ip){
  if(ip!=null && !ip.equals(""))
  {
   try {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(
      "http://api.ipinfodb.com/v3/ip-city/?key={YOUR_API_KEY}&ip=" + ip);
    HttpResponse response = httpClient.execute(postRequest);
 
    if (response.getStatusLine().getStatusCode() != 201) {
     /*
      * throw new RuntimeException("Failed : HTTP error code : " +
      * response.getStatusLine().getStatusCode());
      */
     System.out.println("StatusCode: "
       + response.getStatusLine().getStatusCode());
    }
 
    BufferedReader br = new BufferedReader(new InputStreamReader(
      (response.getEntity().getContent())));
 
    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
     System.out.println(output);
    }
 
    httpClient.getConnectionManager().shutdown();
   } catch (MalformedURLException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}



OUTPUT:

StatusCode: 200
Output from Server .... 

OK;;10.162.1.1;IN;INDIA;GUJARAT;AHMEDABAD;-;23.0333;72.6167;+05:30
IP Info DB


Thats it!
Categories: , , , , ,

6 comments:

  1. Nice valuable information….I got my IP address details from IP-Details.com

    ReplyDelete
  2. Great article...thanks a lot sunil..after a week struggle i found ur site..and it made things so easy

    ReplyDelete
  3. After selecting google map location, it will be displayed on store front.OCMOD extension.Gmap Geo Address

    ReplyDelete
  4. There are a few methods for finding your Ethernet and interchanges convention data.what is myip

    ReplyDelete
  5. Many people may have heard the term IP Address, but few know what it actually means or how locating an IP Address can benefit a business. myip

    ReplyDelete
  6. Thus, a general thought with respect to their clients' decision and perusing patterns is determined.
    what is the new wearable technology

    ReplyDelete

Find me on Facebook! Follow me on Twitter!