Press enter to see results or esc to cancel.


JAVA-Send URL HTTP Request and Read XML Response

In the below code I have shown how to read XML response after sending HTTP URL request using JAVA.
For testing we have used Google Maps TimeZone API which returns XML response with the time zone of requested Coordinates.

Get the API key and documentation from the below link.
https://developers.google.com/maps/documentation/timezone/start

  1. Test_HTTP_XML.java
    package XML_TEST;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.StringReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    public class Test_HTTP_XML {
    public static void main(String[] args) {
     Test_HTTP_XML oobj_Test_HTTP_XML=new Test_HTTP_XML();
     oobj_Test_HTTP_XML.get_response();
    }
    public void get_response(){
    try {
     String api_key="Update API Key here";
     String cordinates="23.023047,81.073447";
     String format="xml";
     String url = "https://maps.googleapis.com/maps/api/timezone/"+format+"?location="+cordinates+"&timestamp=1458000000&key="+api_key;
     System.out.println(url);
     URL obj = new URL(url);
     HttpURLConnection con = (HttpURLConnection) obj.openConnection();
     int responseCode = con.getResponseCode();
     System.out.println("Response Code : " + responseCode);
      BufferedReader in = new BufferedReader(
    	 new InputStreamReader(con.getInputStream()));
    	 String inputLine;
    	 StringBuffer response = new StringBuffer();
    	 while ((inputLine = in.readLine()) != null) {
    	   response.append(inputLine);
    	 }
    	in.close();
    	//print in String
    	// System.out.println(response.toString());
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
             .parse(new InputSource(new StringReader(response.toString())));
    	NodeList errNodes = doc.getElementsByTagName("TimeZoneResponse");
            if (errNodes.getLength() > 0) {
              Element err = (Element)errNodes.item(0);
              System.out.println("raw_offset -"+err.getElementsByTagName("raw_offset").item(0).getTextContent());
    	  System.out.println("dst_offset -"+err.getElementsByTagName("dst_offset").item(0).getTextContent());
              System.out.println("time_zone_id -"+err.getElementsByTagName("time_zone_id").item(0).getTextContent());
              System.out.println("time_zone_name -"+err.getElementsByTagName("time_zone_name").item(0).getTextContent());
    	} else { 
    		     // success
             }
    	} catch (Exception e) {
    	   System.out.println(e);
    	}
    	}
    }
    
  2. XML Response
    <TimeZoneResponse>
     <status>OK</status>
     <raw_offset>19800.0000000</raw_offset>
     <dst_offset>0.0000000</dst_offset>
     <time_zone_id>Asia/Calcutta</time_zone_id>
     <time_zone_name>India Standard Time</time_zone_name>
    </TimeZoneResponse>
    
  3. Console Output
    https://maps.googleapis.com/maps/api/timezone/xml?location=23.023047,81.073447&timestamp=1458000000&key=AIzaSyBWouAFmXPVE7sKpmq-xxPLgxmNb-q5THe
    Response Code : 200
    raw_offset -19800.0000000
    dst_offset -0.0000000
    time_zone_id -Asia/Calcutta
    time_zone_name -India Standard Time