How to Call Restfull API Using Java


Following post I am going to explain how to call Restful API using java and retrieve data

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStoreException;

import javax.net.SocketFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.apache.commons.lang.StringUtils;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;



public class RstAPICall {

                                public static void main(String[] args) {

                                  try {
                                                URL url = new URL("Enter your Restful API url");
                                                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                                               
                                                SocketFactory factory = SSLSocketFactory.getDefault()
                                                conn.setSSLSocketFactory();
                                                //conn.
                                                conn.setRequestMethod("GET");
                                                conn.setRequestProperty("Accept", "application/xml");

                                                if (conn.getResponseCode() != 200) {
                                                                throw new RuntimeException("Failed : HTTP error code : "
                                                                                                + conn.getResponseCode());
                                                }

                                                BufferedReader br = new BufferedReader(new InputStreamReader(
                                                                (conn.getInputStream())));
                                               
                                                String output;
                                                System.out.println("Output from Server .... \n");
                                                 JSONParser parser = new JSONParser();
                                                while ((output = br.readLine()) != null) {
                                                                System.out.println(output);
                                                                OutputString=OutputString + output;



                                                                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                                                                InputSource src = new InputSource();
                                                                src.setCharacterStream(new StringReader(output));

                                                                Document doc = builder.parse(src);
                                               
                                        String SizeCode = StringUtils.substringBetween(output, "<SizeCode>","</SizeCode>");
                                        String SizeName = StringUtils.substringBetween(output, "<SizeWidthName>","</SizeWidthName>");
                                        String SizeDesc = StringUtils.substringBetween(output, "<SizeWidthDescription>","</SizeWidthDescription>");
                                        String NRFsize = StringUtils.substringBetween(output, "<SizeWidthNRF>","</SizeWidthNRF>");
                                       
                                        System.out.println("sizecode:"+SizeCode);
                                        System.out.println("sizename:"+SizeName);
                                        System.out.println("sizedesc:"+SizeDesc);
                                        System.out.println("nrfsize:"+NRFsize);
                                        //System.out.println();
                                         
                                                }
                                               
                                                conn.disconnect();

                                  } catch (MalformedURLException e) {

                                                e.printStackTrace();

                                  } catch (IOException e) {

                                                e.printStackTrace();

                                  } catch(Exception ex){}

                                }


}

Note:
URL url = new URL("Enter your Restful API url");
Enter your restfull API URL Details.

In this example I have retrieve data to xml format. If you want to get output as  Json
conn.setRequestProperty("Accept", "application/json");  







Comments