Skip to main content
1-Visitor
May 18, 2017
Solved

I want to retrive a data from Thingworx platform to java SDK. Can anyone please suggest me how to do it. Please do the needfull.

  • May 18, 2017
  • 3 replies
  • 4728 views

I want to retrive a data from Thingworx platform to java SDK. Can anyone please suggest me how to do it. Please do the needfull.

    Best answer by vr-6

    Hi,

    import java.io.BufferedReader;

    import java.io.DataOutputStream;

    import java.io.InputStreamReader;

    import java.net.HttpURLConnection;

    import java.net.URL;

    import javax.net.ssl.HttpsURLConnection;

    public class HttpURLConnectionExample {

        private final String USER_AGENT = "Mozilla/5.0";

        public static void main(String[] args) throws Exception {

            HttpURLConnectionExample http = new HttpURLConnectionExample();

            System.out.println("Testing 1 - Send Http GET request");

            http.sendGet();

        }

        // HTTP GET request

        private void sendGet() throws Exception {

            String url = "http://localhost:9090/Thingworx/Things/Thingname/Properties?appKey=9d60cdb1-3761-4c85-8fa9-3485b71e6f97";

            URL obj = new URL(url);

            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // optional default is GET

            con.setRequestMethod("GET");

            //add request header

            con.setRequestProperty("User-Agent", USER_AGENT);

                    con.setRequestProperty("Accept", "application/json");

            int responseCode = con.getResponseCode();

            System.out.println("\nSending 'GET' request to URL : " + url);

            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 result

            System.out.println(response.toString());

        }

    }

    You can use above code to print properties in java. Response will be in JSON format.

    3 replies

    20-Turquoise
    May 18, 2017

    Depends on what your end goals are/what you are trying to achieve.

    There are multiple ways:

    • using REST API of the Plattform, pull data from the platform - PTC - helpcenter - it is very straightforward and can be tested with every Browser.
    • using PropertySetters in SDK; Edge helpcenter PTC
    • using services triggerd in the plattform sending data to the device (PUSH)
    1-Visitor
    May 18, 2017

    Hi,

    You can use GET method to retrieve current property value of thing

    /////////////

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()

      .url("http://localhost:9090/Thingworx/Things/ThingName/Properties")

      .get()

      .addHeader("appKey", "8")

      .addHeader("content-type", "application/json")

      .addHeader("accept", "application/json")

      .addHeader("cache-control", "no-cache")

      .build();

    Response response = client.newCall(request).execute();

    //////////////

    To retrieve property history you need to use POST method

    ///////////////////

    OkHttpClient client = new OkHttpClient();

    MediaType mediaType = MediaType.parse("application/json");

    RequestBody body = RequestBody.create(mediaType, "{\n\t\"maxItems\": 500,\n\t\"startDate\":1495119100,\n\t\"endDate\":1495119875,\n\t\"oldestFirst\":true\n}"); // Start Date and End Date UNIX timeformat

    Request request = new Request.Builder()

      .url("http://localhost:9090/Thingworx/Things/ThingName/Services/QueryPropertyHistory?Content-Type=application%2Fjson")

      .post(body)

      .addHeader("appKey", "ccccccc")

      .addHeader("content-type", "application/json")

      .addHeader("accept", "application/json")

      .addHeader("cache-control", "no-cache")

      .build();

    Response response = client.newCall(request).execute();

    /////////////////////////////

    Replace appKey and ThingName

    gd111-VisitorAuthor
    1-Visitor
    May 22, 2017

    Hi,

    Thank you for your response.

    I have tried with GET method. In browser i am getting the properties of the particular thing but when i am invoking URL in code , not able to retrive the properties. I need to print the properties in my java code. How can i get ?? Any idea?

    vr-61-VisitorAnswer
    1-Visitor
    May 22, 2017

    Hi,

    import java.io.BufferedReader;

    import java.io.DataOutputStream;

    import java.io.InputStreamReader;

    import java.net.HttpURLConnection;

    import java.net.URL;

    import javax.net.ssl.HttpsURLConnection;

    public class HttpURLConnectionExample {

        private final String USER_AGENT = "Mozilla/5.0";

        public static void main(String[] args) throws Exception {

            HttpURLConnectionExample http = new HttpURLConnectionExample();

            System.out.println("Testing 1 - Send Http GET request");

            http.sendGet();

        }

        // HTTP GET request

        private void sendGet() throws Exception {

            String url = "http://localhost:9090/Thingworx/Things/Thingname/Properties?appKey=9d60cdb1-3761-4c85-8fa9-3485b71e6f97";

            URL obj = new URL(url);

            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // optional default is GET

            con.setRequestMethod("GET");

            //add request header

            con.setRequestProperty("User-Agent", USER_AGENT);

                    con.setRequestProperty("Accept", "application/json");

            int responseCode = con.getResponseCode();

            System.out.println("\nSending 'GET' request to URL : " + url);

            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 result

            System.out.println(response.toString());

        }

    }

    You can use above code to print properties in java. Response will be in JSON format.

    gd111-VisitorAuthor
    1-Visitor
    May 24, 2017

    Hi,

    Thank you for your response.

    One more doubt i have, I have created a Mashup with one textbox and button. I will enter value in textbox and i will submit the button. I want get the entered value in JavaSDK. Any idea? please ASAP.

    Thank you