You get a bonus - 1 coin for daily activity. Now you have 1 coin

IP Geolocation in Java Using the IpGeoBase Service

Practice



To determine a location by IP using the IpGeoBase service, we will implement:

  • A Location class whose fields will hold the retrieved data
  • A getIpGeoBaseDataByIp() method for obtaining a Location object
  • A makeGetRequest() method for performing the GET request
  • An IpGeoBaseLocation class, into which the data received from ipgeobase.ru will be unmarshalled

 

Here is what the straightforward Location class looks like:

 

We also recommend reading: Converting a number to its text representation in JavaScript

The method for obtaining the location:

 

We create a Location instance that we will later fill with data. We obtain a JAXBContext instance by calling the static newInstance() method, passing it the class to which the XML response will be “bound”, then create an unmarshaller and call its unmarshal() method, passing in a byte stream of the received XML document – as a result, after casting to IpGeoBaseLocation, we use that ipGeoBaseLocation instance to populate our location object and return it.

The method used to perform the GET request:

The IpGeoBaseLocation class:

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Software and information systems development"

Terms: Software and information systems development