Example usage for com.google.gson JsonArray iterator

List of usage examples for com.google.gson JsonArray iterator

Introduction

In this page you can find the example usage for com.google.gson JsonArray iterator.

Prototype

public Iterator<JsonElement> iterator() 

Source Link

Document

Returns an iterator to navigate the elements of the array.

Usage

From source file:com.ibm.iotf.sample.client.application.api.SampleBulkAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to add array of devices to IBM Watson IoT Platform.
 * @throws Exception//from  w  w  w. java2s .com
 */
private void addDevices() throws IoTFCReSTException {
    System.out.println("Adding couple of devices");
    JsonElement input = new JsonParser().parse(deviceToBeAdded);
    JsonArray arryOfDevicesToBeAdded = new JsonArray();
    arryOfDevicesToBeAdded.add(input);
    try {
        JsonArray devices = this.apiClient.addMultipleDevices(arryOfDevicesToBeAdded);
        for (Iterator<JsonElement> iterator = devices.iterator(); iterator.hasNext();) {
            JsonElement deviceElement = iterator.next();
            JsonObject responseJson = deviceElement.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());

        // Print if there is a partial response
        System.out.println(e.getResponse());
    }
}

From source file:com.ibm.iotf.sample.client.application.api.SampleBulkAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to get array of devices registered in the organization, sorted by deviceId.
 * @throws Exception/*from   w  w w.  ja  va2  s . c  o  m*/
 */
private void getAllDevices() throws IoTFCReSTException {
    System.out.println("Retrieve all devices in the Organization..");
    // Get all the devices in the organization
    /**
     * The Java ibmiotf client library provides an one argument constructor
     * which can be used to control the output, for example, lets try to retrieve
     * the devices in a sorted order based on device ID.
     */

    ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("_sort", "deviceId"));
    //parameters.add(new BasicNameValuePair("_limit","2"));
    JsonObject response = this.apiClient.getAllDevices(parameters);

    System.out.println(response);
    // The response will contain more parameters that will be used to issue
    // the next request. The result element will contain the current list of devices
    JsonArray devices = response.get("results").getAsJsonArray();
    for (Iterator<JsonElement> iterator = devices.iterator(); iterator.hasNext();) {
        JsonElement deviceElement = iterator.next();
        JsonObject responseJson = deviceElement.getAsJsonObject();
        System.out.println(responseJson);
    }
}

From source file:com.ibm.iotf.sample.client.application.api.SampleDeviceAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to retrieve all the devices in an organization using the Java Client Library.
 * @throws IoTFCReSTException/*from  ww w. ja  va 2s. co m*/
 */
private void getAllDevices() throws IoTFCReSTException {
    System.out.println("Get all devices of device type--> " + DEVICE_TYPE);
    // Get all the devices of type SampleDT
    try {
        /**
         * The Java ibmiotf client library provides an one argument constructor
         * which can be used to control the output, for example, lets try to retrieve
         * the devices in a sorted order based on device ID.
         */

        ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("_sort", "deviceId"));

        JsonObject response = this.apiClient.retrieveDevices(DEVICE_TYPE, parameters);

        // The response will contain more parameters that will be used to issue
        // the next request. The result element will contain the current list of devices
        JsonArray devices = response.get("results").getAsJsonArray();
        for (Iterator<JsonElement> iterator = devices.iterator(); iterator.hasNext();) {
            JsonElement deviceElement = iterator.next();
            JsonObject responseJson = deviceElement.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());
        // Print if there is a partial response
        System.out.println(e.getResponse());
    }
}

From source file:com.ibm.iotf.sample.client.application.api.SampleDeviceDiagnosticsAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to get all the diagnostic Logs of a device.
 * @throws IoTFCReSTException/*from   w w w.j  av  a2  s.  c o  m*/
 */
private void getAllDiagnosticLogs() {
    System.out.println("Get all diag Log of device --> " + DEVICE_ID + " of type " + DEVICE_TYPE);
    // Get all the diagnostics logs of device DEVICE_ID
    try {
        JsonArray response = this.apiClient.getAllDiagnosticLogs(DEVICE_TYPE, DEVICE_ID);
        for (Iterator<JsonElement> iterator = response.iterator(); iterator.hasNext();) {
            JsonElement deviceElement = iterator.next();
            JsonObject responseJson = deviceElement.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());
        // Print if there is a partial response
        System.out.println(e.getResponse());
    }
}

From source file:com.ibm.iotf.sample.client.application.api.SampleDeviceDiagnosticsAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to get all the diagnostic error codes.
 * @throws IoTFCReSTException/*from  w w  w . j  a  v  a 2s.  c o  m*/
 */
private void getAllDiagnosticErrorCodes() {
    System.out.println("Get all diag Errorcodes of device --> " + DEVICE_ID + " of type " + DEVICE_TYPE);
    // Get all the diagnostics ErrorCodes of device DEVICE_ID
    try {
        JsonArray response = this.apiClient.getAllDiagnosticErrorCodes(DEVICE_TYPE, DEVICE_ID);
        for (Iterator<JsonElement> iterator = response.iterator(); iterator.hasNext();) {
            JsonElement deviceElement = iterator.next();
            JsonObject responseJson = deviceElement.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());
        // Print if there is a partial response
        System.out.println(e.getResponse());
    }
}

From source file:com.ibm.iotf.sample.client.application.api.SampleDeviceDiagnosticsAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to get connection logs which is not related to the Diagnostic Log.
 *//*from  w w  w.ja  v a 2s. co  m*/
private void getDeviceConnectionLogs() {
    System.out.println("Get device connection logs for device --> " + DEVICE_ID + " of type " + DEVICE_TYPE);
    // Get the connection logs of device DEVICE_ID
    try {
        JsonArray response = this.apiClient.getDeviceConnectionLogs(DEVICE_TYPE, DEVICE_ID);
        for (Iterator<JsonElement> iterator = response.iterator(); iterator.hasNext();) {
            JsonElement deviceElement = iterator.next();
            JsonObject responseJson = deviceElement.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());
        // Print if there is a partial response
        System.out.println(e.getResponse());
    }
}

From source file:com.ibm.iotf.sample.client.application.api.SampleDeviceManagementAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to get a list of device management requests, which can be in progress or recently completed.
 * @throws IoTFCReSTException/*from   ww  w  .  j  a v  a2s .  c  o  m*/
 */
private void getAllMgmtRequests() throws IoTFCReSTException {
    System.out.println("Retrieve all DM requests from the organization..");
    try {
        JsonElement response = this.apiClient.getAllDeviceManagementRequests();
        JsonArray requests = response.getAsJsonObject().get("results").getAsJsonArray();
        for (Iterator<JsonElement> iterator = requests.iterator(); iterator.hasNext();) {
            JsonElement request = iterator.next();
            JsonObject responseJson = request.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());
        // Print if there is a partial response
        System.out.println(e.getResponse());
    }

}

From source file:com.ibm.iotf.sample.client.application.api.SampleDeviceManagementAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to get list of device management request device statuses
 * @throws IoTFCReSTException/*from w ww . j  av a 2 s  .c om*/
 */
private void getMgmtRequestDeviceStatus() throws IoTFCReSTException {
    // Lets get the DM request status from the list
    System.out.println("Get DM request device status..");
    try {
        JsonElement response = this.apiClient.getAllDeviceManagementRequests();
        JsonArray requests = response.getAsJsonObject().get("results").getAsJsonArray();
        JsonElement request = requests.get(0);
        String id = request.getAsJsonObject().get("id").getAsString();

        ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("_bookmark", "<bookmark>"));

        JsonObject details = this.apiClient.getDeviceManagementRequestStatus(id);

        // The response will contain more parameters that will be used to issue
        // the next request. The results element will contain the current list of devices
        JsonArray devices = details.get("results").getAsJsonArray();
        for (Iterator<JsonElement> iterator = devices.iterator(); iterator.hasNext();) {
            JsonElement deviceElement = iterator.next();
            JsonObject responseJson = deviceElement.getAsJsonObject();
            System.out.println(responseJson);
        }

    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());
        // Print if there is a partial response
        System.out.println(e.getResponse());
    }

}

From source file:com.ibm.iotf.sample.client.application.api.SampleDeviceTypeAPIOperations.java

License:Open Source License

/**
 * This sample showcases how to retrieve all the device types present in the given Organization.
 * @throws IoTFCReSTException/*from   ww  w . ja v a  2s  .  com*/
 */
private void getAllDeviceTypes() throws IoTFCReSTException {
    System.out.println("Get all device types present in the Organization");
    // Get all the devices in the organization
    /**
     * The Java ibmiotf client library provides an one argument constructor
     * which can be used to control the output, for example, lets try to retrieve
     * the devices in a sorted order based on device type ID.
     */

    ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("_sort", "id"));

    try {
        JsonObject response = this.apiClient.getAllDeviceTypes(parameters);

        // The response will contain more parameters that will be used to issue
        // the next request. The result element will contain the current list of devices
        JsonArray deviceTypes = response.get("results").getAsJsonArray();
        for (Iterator<JsonElement> iterator = deviceTypes.iterator(); iterator.hasNext();) {
            JsonElement deviceElement = iterator.next();
            JsonObject responseJson = deviceElement.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (IoTFCReSTException e) {
        System.out.println("HttpCode :" + e.getHttpCode() + " ErrorMessage :: " + e.getMessage());
        // Print if there is a partial response
        System.out.println(e.getResponse());
    }
}

From source file:com.ibm.iotf.sample.client.application.api.SampleHistorianAPIOperations.java

License:Open Source License

/**
 * This sample method retrieves historical events across all devices registered 
 * in the organization with the supplied query parameters.
 *///from   w  w  w. j ava2 s.c o  m
private void getAllHistoricalEventsWithSampleQueryParameter() {
    System.out.println("Get all blink events from start date " + new Date(1445420849839L));
    // Get the historical events 
    try {
        ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("evt_type", "blink"));
        parameters.add(new BasicNameValuePair("start", "1445420849839"));
        JsonElement response = this.apiClient.getHistoricalEvents(parameters);

        // The response will contain more parameters that will be used to issue
        // the next request. The events element will contain the current list of devices
        JsonArray events = response.getAsJsonObject().get("events").getAsJsonArray();
        for (Iterator<JsonElement> iterator = events.iterator(); iterator.hasNext();) {
            JsonElement event = iterator.next();
            JsonObject responseJson = event.getAsJsonObject();
            System.out.println(responseJson);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}