cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

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.

gd11
1-Newbie

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.

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.

1 ACCEPTED SOLUTION

Accepted Solutions
vr-6
4-Participant
(To:gd11)

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.

View solution in original post

6 REPLIES 6
posipova
20-Turquoise
(To:gd11)

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)
vr-6
4-Participant
(To:gd11)

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

gd11
1-Newbie
(To:gd11)

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-6
4-Participant
(To:gd11)

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.

gd11
1-Newbie
(To:vr-6)

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

vr-6
4-Participant
(To:gd11)

Hi,

You can use change link to get particular property value in JAVA SDK.

Example:

localhost:9090/Thingworx/Things/THINGNAME/Properties/PROPERTYNAME?appKey=9d60cdb1-3761-4c85-8fa9-3485b71e6f97

Top Tags