Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. 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.
Solved! Go to Solution.
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.
Depends on what your end goals are/what you are trying to achieve.
There are multiple ways:
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()
.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
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?
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.
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
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