Hi Abhishek,
please see the following (from engineering). HTH.
In most simple case it could be done using the code below, but I would recommend use Apache Olingo client library https://olingo.apache.org/doc/odata2/tutorials/OlingoV2BasicClientSample.html
public static final String PASSWORD="admin";
public static final String USERNAME="admin";
public static final String METADATA = "$metadata";
public static final String HTTP_METHOD_PUT = "PUT";
public static final String HTTP_METHOD_POST = "POST";
public static final String HTTP_METHOD_GET = "GET";
public static final String HTTP_HEADER_CONTENT_TYPE = "Content-Type";
public static final String HTTP_HEADER_ACCEPT = "Accept";
public static final String APPLICATION_JSON = "application/json";
public static final String APPLICATION_XML = "application/xml";
public static HttpURLConnection connectToURL(URL url, String contentType, String httpMethod)
throws IOException
{
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String credentials = TestConstants.USERNAME + ":" + TestConstants.PASSWORD;
String encoding = new sun.misc.BASE64Encoder().encode(credentials.getBytes());
connection.setRequestProperty("Authorization", "Basic " + encoding);
connection.setRequestProperty(TestConstants.HTTP_HEADER_ACCEPT, contentType);
connection.setRequestMethod(httpMethod);
return (connection);
}
public static HttpURLConnection readDataFromServer(URL url, String contentType)
throws Exception
{
// Connect to url
HttpURLConnection connection = connectToURL(url, contentType, TestConstants.HTTP_METHOD_GET);
// Get the result
int responseCode = ((HttpURLConnection) connection).getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK)
{
// Buffer to read
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
// Just read off the data, but do not store for now.
String inputLine;
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
System.out.println(response);
}
return(connection);
}
public static HttpURLConnection readXMLDataFromServer(URL url, String contentType)
throws Exception
{
// Connect to url
HttpURLConnection connection = connectToURL(url, contentType, TestConstants.HTTP_METHOD_GET);
// Get the result
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(connection.getInputStream());
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 2);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(db.parse(is));
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
}
return(connection);
}
URL url = new URL(“http://hostname:8581/odata/api/devices?”);
// Connect and read from the server
HttpURLConnection connection = ODataUtil.readDataFromServer(url, APPLICATION_JSON);