Search code examples
javarestodatahttpconnectionolingo

Getting the count of entries in entitySet in OData using java


The following link returns the number of entries in the Customers entity Set http://services.odata.org/Northwind/Northwind.svc/Customers/$count

How to get this number using java?

 URL url = new URL("http://services.odata.org/Northwind/Northwind.svc/Customers/$count");
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 conn.setRequestMethod("GET")

What to code after this to get the count of entries as integers?


Solution

  • You need to read the data from HttpURLConnection inputstream like

     BufferedReader in = new BufferedReader(new InputStreamReader(
                                        conn.getInputStream()));
            String count;
            while ((count = in.readLine()) != null) 
                //this will print the count in count variable
                System.out.println(count);
            in.close();
        }
    

    Note : You have to do it after you write the request to the outputstream of the HttpURLConnection. This clearly means that you write the request data to the connection's output stream and read the response data from the connection's input stream