3 Replies Latest reply on Aug 11, 2017 7:20 AM by jamesm RSS
    yzhang Newbie

    How to return a value when create a service in extension

    Hi all,

     

    I am now using Thingworx SDK and eclipse plugin to create thingworx extension.

     

    But I found when I create a service, I don't know how to return value to thingworx.

     

    In pure java environment, I use code below and can get JSON reply:

     

    public class Imagetemp

    {

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

              String httpsURL = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=4d52e94bd14312daee226197d05557697dc58867&version=2016-05-20&url=https://github.com/watson-developer-cloud/doc-tutorial-downloads/raw/master/visual-recognition/fruitbowl.jpg";

               URL myUrl = new URL(httpsURL);

               Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.company.com", 8080));  

                HttpsURLConnection conn = (HttpsURLConnection)myUrl.openConnection(proxy);

                TrustModifier.relaxHostChecking(conn);

                InputStream is = conn.getInputStream();

                InputStreamReader isr = new InputStreamReader(is);

                BufferedReader br = new BufferedReader(isr);

          

            String inputLine;

     

     

            while ((inputLine = br.readLine()) != null) {

                System.out.println(inputLine);

            }

          br.close();

          

    // Add your code below

    }       

    }

     

    And in thingworx project, I copy the code above into logger.trace:

     

    @ThingworxBaseTemplateDefinition(name = "GenericThing")

    @ThingworxPropertyDefinitions(properties = {

    @ThingworxPropertyDefinition(name = "ImageAnal", description = "", category = "", baseType = "STRING", isLocalOnly = false, aspects = {

    "isPersistent:true", "isLogged:true", "dataChangeType:VALUE" }) })

    public class FinalImage extends Thing {

     

     

    private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(FinalImage.class);

     

     

    @ThingworxServiceDefinition(name = "ImageAna", description = "", category = "", isAllowOverride = false, aspects = {

    "isAsync:false" })

    @ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})

    public String ImageAna() throws Exception {

    _logger.trace("Entering Service: ImageAna");

     

            String httpsURL = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=4d52e94bd14312daee226197d05557697dc58867&version=2016-05-20&url=https://github.com/watson-developer-cloud/doc-tutorial-downloads/raw/master/visual-recognition/fruitbowl.jpg";

            URL myUrl = new URL(httpsURL);

            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.micron.com", 8080));

         

            HttpsURLConnection conn = (HttpsURLConnection)myUrl.openConnection(proxy);

            TrustModifier.relaxHostChecking(conn);

            InputStream is = conn.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);

            StringBuilder sb = new StringBuilder();

            String Result = "";

            while ((Result = br.readLine()) != null) {

            System.out.println(Result);

            }

    _logger.trace("Exiting Service: ImageAna");

     

    return Result;

    }

    }

     

     

    But the output is empty.

     

    Can I know how to pass the result "inputline" to Result and get result when I trigger the service. Thanks!

     

    Thanks