Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

In this page you can find the example usage for org.json JSONArray getJSONObject.

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

From source file:com.liferay.mobile.android.v7.ddmstructure.DDMStructureService.java

public JSONObject addStructure(long groupId, long parentStructureId, long classNameId, String structureKey,
        JSONObject nameMap, JSONObject descriptionMap, String xsd, String storageType, int type,
        JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from   w  ww  . j  a va  2s  .  co m
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("parentStructureId", parentStructureId);
        _params.put("classNameId", classNameId);
        _params.put("structureKey", checkNull(structureKey));
        _params.put("nameMap", checkNull(nameMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        _params.put("xsd", checkNull(xsd));
        _params.put("storageType", checkNull(storageType));
        _params.put("type", type);
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.kernel.service.ServiceContext",
                serviceContext);

        _command.put("/ddm.ddmstructure/add-structure", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:com.liferay.mobile.android.v7.ddmstructure.DDMStructureService.java

public JSONObject updateStructure(long structureId, long parentStructureId, JSONObject nameMap,
        JSONObject descriptionMap, String definition, JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {/* w ww.  ja v a2s.  c o  m*/
        JSONObject _params = new JSONObject();

        _params.put("structureId", structureId);
        _params.put("parentStructureId", parentStructureId);
        _params.put("nameMap", checkNull(nameMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        _params.put("definition", checkNull(definition));
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.kernel.service.ServiceContext",
                serviceContext);

        _command.put("/ddm.ddmstructure/update-structure", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:com.liferay.mobile.android.v7.ddmstructure.DDMStructureService.java

public JSONObject updateStructure(long structureId, long parentStructureId, JSONObject nameMap,
        JSONObject descriptionMap, JSONObjectWrapper ddmForm, JSONObjectWrapper ddmFormLayout,
        JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from  w w  w . ja  va 2  s  . c om
        JSONObject _params = new JSONObject();

        _params.put("structureId", structureId);
        _params.put("parentStructureId", parentStructureId);
        _params.put("nameMap", checkNull(nameMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        mangleWrapper(_params, "ddmForm", "com.liferay.dynamic.data.mapping.model.DDMForm", ddmForm);
        mangleWrapper(_params, "ddmFormLayout", "com.liferay.dynamic.data.mapping.model.DDMFormLayout",
                ddmFormLayout);
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.kernel.service.ServiceContext",
                serviceContext);

        _command.put("/ddm.ddmstructure/update-structure", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:com.liferay.mobile.android.v7.ddmstructure.DDMStructureService.java

public JSONObject updateStructure(long groupId, long parentStructureId, long classNameId, String structureKey,
        JSONObject nameMap, JSONObject descriptionMap, JSONObjectWrapper ddmForm,
        JSONObjectWrapper ddmFormLayout, JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {/*  w  ww  . j a  v a2s. c  o  m*/
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("parentStructureId", parentStructureId);
        _params.put("classNameId", classNameId);
        _params.put("structureKey", checkNull(structureKey));
        _params.put("nameMap", checkNull(nameMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        mangleWrapper(_params, "ddmForm", "com.liferay.dynamic.data.mapping.model.DDMForm", ddmForm);
        mangleWrapper(_params, "ddmFormLayout", "com.liferay.dynamic.data.mapping.model.DDMFormLayout",
                ddmFormLayout);
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.kernel.service.ServiceContext",
                serviceContext);

        _command.put("/ddm.ddmstructure/update-structure", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:com.liferay.mobile.android.v7.ddmstructure.DDMStructureService.java

public JSONObject updateStructure(long groupId, long parentStructureId, long classNameId, String structureKey,
        JSONObject nameMap, JSONObject descriptionMap, String definition, JSONObjectWrapper serviceContext)
        throws Exception {
    JSONObject _command = new JSONObject();

    try {//from   www . j a  va  2s.c  o m
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("parentStructureId", parentStructureId);
        _params.put("classNameId", classNameId);
        _params.put("structureKey", checkNull(structureKey));
        _params.put("nameMap", checkNull(nameMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        _params.put("definition", checkNull(definition));
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.kernel.service.ServiceContext",
                serviceContext);

        _command.put("/ddm.ddmstructure/update-structure", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:com.liferay.mobile.android.v7.ddmstructure.DDMStructureService.java

public JSONObject copyStructure(long structureId, JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {// w  w  w  .ja  va  2s . c  o  m
        JSONObject _params = new JSONObject();

        _params.put("structureId", structureId);
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.kernel.service.ServiceContext",
                serviceContext);

        _command.put("/ddm.ddmstructure/copy-structure", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:com.liferay.mobile.android.v7.ddmstructure.DDMStructureService.java

public JSONObject copyStructure(long structureId, JSONObject nameMap, JSONObject descriptionMap,
        JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {//  w  ww  . j  av a 2s  .  co  m
        JSONObject _params = new JSONObject();

        _params.put("structureId", structureId);
        _params.put("nameMap", checkNull(nameMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.kernel.service.ServiceContext",
                serviceContext);

        _command.put("/ddm.ddmstructure/copy-structure", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:GUI.simplePanel.java

public simplePanel() {
    self = this;/*from   ww  w .j a  va2s. co  m*/
    final Microphone mic = new Microphone(FLACFileWriter.FLAC);//Instantiate microphone and have 

    final GSpeechDuplex dup = new GSpeechDuplex("AIzaSyBc-PCGLbT2M_ZBLUPEl9w2OY7jXl90Hbc");//Instantiate the API
    dup.addResponseListener(new GSpeechResponseListener() {// Adds the listener
        public void onResponse(GoogleResponse gr) {
            System.out.println("got response");
            jTextArea1.setText(gr.getResponse() + "\n" + jTextArea1.getText());

            getjLabel1().setText("Awaiting Command");
            if (gr.getResponse().contains("temperature")) {
                try {
                    String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
                    JSONObject obj;

                    obj = new JSONObject(reply);
                    JSONArray services = obj.getJSONArray("services");
                    boolean found = false;
                    for (int i = 0; i < services.length(); i++) {
                        if (found) {
                            return;
                        }
                        Object pref = services.getJSONObject(i).get("url");
                        String url = (String) pref;
                        if (url.contains("temp")) {
                            // http://127.0.0.1:8181/sensor/1/temp
                            String serviceHost = (url.split(":")[1].substring(2));
                            int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                            String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                            String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                            JSONObject temperature;

                            obj = new JSONObject(serviceReply);
                            String temp = obj.getJSONObject("sensor").getString("Temperature");
                            JOptionPane.showMessageDialog(self,
                                    "Temperature is " + temp.substring(0, temp.indexOf(".") + 2) + " Celsius");
                            found = true;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (gr.getResponse().contains("light") || gr.getResponse().startsWith("li")) {
                try {
                    String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
                    JSONObject obj;

                    obj = new JSONObject(reply);
                    JSONArray services = obj.getJSONArray("services");
                    boolean found = false;
                    for (int i = 0; i < services.length(); i++) {
                        if (found) {
                            return;
                        }
                        Object pref = services.getJSONObject(i).get("url");
                        String url = (String) pref;
                        if (url.contains("light")) {
                            // http://127.0.0.1:8181/sensor/1/temp
                            String serviceHost = (url.split(":")[1].substring(2));
                            int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                            String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                            String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                            JSONObject temperature;

                            obj = new JSONObject(serviceReply);
                            String temp = obj.getJSONObject("sensor").getString("Light");
                            JOptionPane.showMessageDialog(self, "Light levels are at " + temp + " of 1023 ");
                            found = true;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else if ((gr.getResponse().contains("turn on") || gr.getResponse().contains("turn off"))
                    && gr.getResponse().contains("number")) {
                int numberIndex = gr.getResponse().indexOf("number ") + "number ".length();
                String number = gr.getResponse().substring(numberIndex).split(" ")[0];
                if (number.equals("for") || number.equals("four")) {
                    number = "4";
                }
                if (number.equals("to") || number.equals("two") || number.equals("cho")) {
                    number = "2";
                }
                if (number.equals("one")) {
                    number = "1";
                }
                if (number.equals("three")) {
                    number = "3";
                }

                try {
                    String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
                    JSONObject obj;

                    obj = new JSONObject(reply);
                    JSONArray services = obj.getJSONArray("services");
                    boolean found = false;
                    for (int i = 0; i < services.length(); i++) {
                        if (found) {
                            return;
                        }
                        Object pref = services.getJSONObject(i).get("url");
                        String url = (String) pref;
                        if (url.contains("sensor/" + number)) {
                            // http://127.0.0.1:8181/sensor/1/temp
                            String serviceHost = (url.split(":")[1].substring(2));
                            int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                            String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                            String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                            JSONObject temperature;

                            obj = new JSONObject(serviceReply);
                            String temp = obj.getJSONObject("sensor").getString("Switch");
                            if (!(temp.equals("0") || temp.equals("1"))) {
                                JOptionPane.showMessageDialog(self,
                                        "Sensor does not provide a switch service man");
                            } else if (gr.getResponse().contains("turn on") && temp.equals("1")) {
                                JOptionPane.showMessageDialog(self,
                                        "Switch is already on at sensor " + number + "!");
                            } else if (gr.getResponse().contains("turn off") && temp.equals("0")) {
                                JOptionPane.showMessageDialog(self,
                                        "Switch is already off at sensor " + number + "!");
                            } else if (gr.getResponse().contains("turn on") && temp.equals("0")) {
                                String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "",
                                        "/sensor/" + number + "/switch");
                                JOptionPane.showMessageDialog(self, "Request for switch sent");
                            } else if (gr.getResponse().contains("turn off") && temp.equals("1")) {
                                String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "",
                                        "/sensor/" + number + "/switch");
                                JOptionPane.showMessageDialog(self, "Request for switch sent");
                            }

                            found = true;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else if (gr.getResponse().contains("change") && gr.getResponse().contains("number")) {

                int numberIndex = gr.getResponse().indexOf("number ") + "number ".length();
                String number = gr.getResponse().substring(numberIndex).split(" ")[0];
                if (number.equals("for") || number.equals("four")) {
                    number = "4";
                }
                if (number.equals("to") || number.equals("two") || number.equals("cho")) {
                    number = "2";
                }
                if (number.equals("one")) {
                    number = "1";
                }
                if (number.equals("three")) {
                    number = "3";
                }

                try {
                    String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
                    JSONObject obj;

                    obj = new JSONObject(reply);
                    JSONArray services = obj.getJSONArray("services");
                    boolean found = false;
                    for (int i = 0; i < services.length(); i++) {
                        if (found) {
                            return;
                        }
                        Object pref = services.getJSONObject(i).get("url");
                        String url = (String) pref;
                        if (url.contains("sensor/" + number)) {
                            // http://127.0.0.1:8181/sensor/1/temp
                            String serviceHost = (url.split(":")[1].substring(2));
                            int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                            String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                            String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                            JSONObject temperature;

                            obj = new JSONObject(serviceReply);
                            String temp = obj.getJSONObject("sensor").getString("Switch");
                            if (!(temp.equals("0") || temp.equals("1"))) {
                                JOptionPane.showMessageDialog(self,
                                        "Sensor does not provide a switch service man");
                            } else {
                                String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "",
                                        "/sensor/" + number + "/switch");
                                JOptionPane.showMessageDialog(self, "Request for switch sent");
                            }

                            found = true;
                        }
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(self, e.getLocalizedMessage());
                }
            } else if (gr.getResponse().contains("get all")) {

                try {
                    String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
                    JSONObject obj;

                    obj = new JSONObject(reply);
                    JSONArray services = obj.getJSONArray("services");
                    boolean found = false;
                    String servicesString = "";
                    for (int i = 0; i < services.length(); i++) {
                        Object pref = services.getJSONObject(i).get("url");
                        String url = (String) pref;
                        servicesString += url + "\n";

                    }
                    JOptionPane.showMessageDialog(self, servicesString);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                try {
                    ChatterBotFactory factory = new ChatterBotFactory();
                    ChatterBot bot1 = factory.create(CLEVERBOT);
                    ChatterBotSession bot1session = bot1.createSession();
                    String s = gr.getResponse();
                    String response = bot1session.think(s);
                    JOptionPane.showMessageDialog(self, response);
                } catch (Exception e) {
                }
            }
            System.out.println("Google thinks you said: " + gr.getResponse());
            System.out.println("with "
                    + ((gr.getConfidence() != null) ? (Double.parseDouble(gr.getConfidence()) * 100) : null)
                    + "% confidence.");
            System.out.println("Google also thinks that you might have said:" + gr.getOtherPossibleResponses());
        }
    });
    initComponents();
    jTextField1.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                String input = jTextField1.getText();
                jTextField1.setText("");
                textParser(input);
            }
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void keyReleased(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    });
    jButton1.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mousePressed(MouseEvent e) {
            // it record FLAC file.
            File file = new File("CRAudioTest.flac");//The File to record the buffer to. 
            //You can also create your own buffer using the getTargetDataLine() method.
            System.out.println("Start Talking Honey");
            try {
                mic.captureAudioToFile(file);//Begins recording
            } catch (Exception ex) {
                ex.printStackTrace();//Prints an error if something goes wrong.
            }
            //System.out.println("You can stop now");
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            try {
                mic.close();//Stops recording
                //Sends 10 second voice recording to Google
                byte[] data = Files.readAllBytes(mic.getAudioFile().toPath());//Saves data into memory.
                dup.recognize(data, (int) mic.getAudioFormat().getSampleRate(), self);
                //mic.getAudioFile().delete();//Deletes Buffer file
                //REPEAT
            } catch (Exception ex) {
                ex.printStackTrace();//Prints an error if something goes wrong.
            }
            System.out.println("You can stop now");

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseExited(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    setVisible(true);

}

From source file:GUI.simplePanel.java

public void textParser(String input) {
    boolean served = false;
    jTextArea1.setText(input + "\n" + jTextArea1.getText());

    getjLabel1().setText("Awaiting Command");
    if (input.contains("temperature")) {
        try {/*from www .  jav a  2  s  . co  m*/
            String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
            JSONObject obj;

            obj = new JSONObject(reply);
            JSONArray services = obj.getJSONArray("services");
            boolean found = false;
            for (int i = 0; i < services.length(); i++) {
                if (found) {
                    served = true;
                    return;
                }
                Object pref = services.getJSONObject(i).get("url");
                String url = (String) pref;
                if (url.contains("temp")) {
                    // http://127.0.0.1:8181/sensor/1/temp
                    String serviceHost = (url.split(":")[1].substring(2));
                    int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                    String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                    String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                    JSONObject temperature;

                    obj = new JSONObject(serviceReply);
                    String temp = obj.getJSONObject("sensor").getString("Temperature");
                    if (temp.length() > 4) {
                        JOptionPane.showMessageDialog(self,
                                "Temperature is " + temp.substring(0, temp.indexOf(".") + 2) + " Celsius");
                    } else {
                        JOptionPane.showMessageDialog(self, "Temperature is " + temp + " Celsius");
                    }

                    found = true;
                }
            }
            JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (input.contains("light") || input.startsWith("li")) {
        try {
            String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
            JSONObject obj;

            obj = new JSONObject(reply);
            JSONArray services = obj.getJSONArray("services");
            boolean found = false;
            for (int i = 0; i < services.length(); i++) {
                if (found) {
                    served = true;
                    return;
                }
                Object pref = services.getJSONObject(i).get("url");
                String url = (String) pref;
                if (url.contains("light")) {
                    // http://127.0.0.1:8181/sensor/1/temp
                    String serviceHost = (url.split(":")[1].substring(2));
                    int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                    String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                    String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                    JSONObject temperature;

                    obj = new JSONObject(serviceReply);
                    String temp = obj.getJSONObject("sensor").getString("Light");
                    JOptionPane.showMessageDialog(self, "Light levels are at " + temp + " of 1023 ");
                    found = true;
                }
            }
            JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    if ((input.contains("turn on") || input.contains("turn off")) && input.contains("number")) {
        int numberIndex = input.indexOf("number ") + "number ".length();
        String number = input.substring(numberIndex).split(" ")[0];
        if (number.equals("for") || number.equals("four")) {
            number = "4";
        }
        if (number.equals("to") || number.equals("two") || number.equals("cho")) {
            number = "2";
        }
        if (number.equals("one")) {
            number = "1";
        }
        if (number.equals("three")) {
            number = "3";
        }

        try {
            String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
            JSONObject obj;

            obj = new JSONObject(reply);
            JSONArray services = obj.getJSONArray("services");
            boolean found = false;
            for (int i = 0; i < services.length(); i++) {
                if (found) {
                    served = true;
                    return;
                }
                Object pref = services.getJSONObject(i).get("url");
                String url = (String) pref;
                if (url.contains("sensor/" + number)) {
                    // http://127.0.0.1:8181/sensor/1/temp
                    String serviceHost = (url.split(":")[1].substring(2));
                    int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                    String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                    String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                    JSONObject temperature;

                    obj = new JSONObject(serviceReply);
                    String temp = obj.getJSONObject("sensor").getString("Switch");
                    if (!(temp.equals("0") || temp.equals("1"))) {
                        JOptionPane.showMessageDialog(self, "Sensor does not provide a switch service man");
                    } else if (input.contains("turn on") && temp.equals("1")) {
                        JOptionPane.showMessageDialog(self, "Switch is already on at sensor " + number + "!");
                    } else if (input.contains("turn off") && temp.equals("0")) {
                        JOptionPane.showMessageDialog(self, "Switch is already off at sensor " + number + "!");
                    } else if (input.contains("turn on") && temp.equals("0")) {
                        String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "",
                                "/sensor/" + number + "/switch");
                        JOptionPane.showMessageDialog(self, "Request for switch sent");
                    } else if (input.contains("turn off") && temp.equals("1")) {
                        String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "",
                                "/sensor/" + number + "/switch");
                        JOptionPane.showMessageDialog(self, "Request for switch sent");
                    }

                    found = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    if (input.contains("change") && input.contains("number")) {

        int numberIndex = input.indexOf("number ") + "number ".length();
        String number = input.substring(numberIndex).split(" ")[0];
        if (number.equals("for") || number.equals("four")) {
            number = "4";
        }
        if (number.equals("to") || number.equals("two") || number.equals("cho")) {
            number = "2";
        }
        if (number.equals("one")) {
            number = "1";
        }
        if (number.equals("three")) {
            number = "3";
        }

        try {
            String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
            JSONObject obj;

            obj = new JSONObject(reply);
            JSONArray services = obj.getJSONArray("services");
            boolean found = false;
            for (int i = 0; i < services.length(); i++) {
                if (found) {
                    served = true;
                    return;
                }
                Object pref = services.getJSONObject(i).get("url");
                String url = (String) pref;
                if (url.contains("sensor/" + number)) {
                    // http://127.0.0.1:8181/sensor/1/temp
                    String serviceHost = (url.split(":")[1].substring(2));
                    int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                    String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                    String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                    JSONObject temperature;

                    obj = new JSONObject(serviceReply);
                    String temp = obj.getJSONObject("sensor").getString("Switch");
                    if (!(temp.equals("0") || temp.equals("1"))) {
                        JOptionPane.showMessageDialog(self, "Sensor does not provide a switch service man");
                    } else {
                        String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "",
                                "/sensor/" + number + "/switch");
                        JOptionPane.showMessageDialog(self, "Request for switch sent");
                    }

                    found = true;
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(self, e.getLocalizedMessage());
        }
    }
    if (input.contains("get all")) {

        try {
            String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
            JSONObject obj;
            served = true;
            obj = new JSONObject(reply);
            JSONArray services = obj.getJSONArray("services");
            boolean found = false;
            String servicesString = "";
            for (int i = 0; i < services.length(); i++) {
                Object pref = services.getJSONObject(i).get("url");
                String url = (String) pref;
                servicesString += url + "\n";

            }
            JOptionPane.showMessageDialog(self, servicesString);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    if (input.contains("humidity") || input.startsWith("humidity")) {
        try {
            String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
            JSONObject obj;

            obj = new JSONObject(reply);
            JSONArray services = obj.getJSONArray("services");
            boolean found = false;
            for (int i = 0; i < services.length(); i++) {
                if (found) {
                    served = true;
                    return;
                }
                Object pref = services.getJSONObject(i).get("url");
                String url = (String) pref;
                if (url.contains("humi")) {
                    // http://127.0.0.1:8181/sensor/1/temp
                    String serviceHost = (url.split(":")[1].substring(2));
                    int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                    String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                    String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                    JSONObject temperature;

                    obj = new JSONObject(serviceReply);
                    String temp = obj.getJSONObject("sensor").getString("Humidity");
                    JOptionPane.showMessageDialog(self, "Humidity levels are at " + temp + " of 1023 ");
                    found = true;
                }
            }
            JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    if (input.contains("pressure") || input.startsWith("pressure")) {
        try {
            String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
            JSONObject obj;

            obj = new JSONObject(reply);
            JSONArray services = obj.getJSONArray("services");
            boolean found = false;
            for (int i = 0; i < services.length(); i++) {
                if (found) {
                    served = true;
                    return;
                }
                Object pref = services.getJSONObject(i).get("url");
                String url = (String) pref;
                if (url.contains("pres")) {
                    // http://127.0.0.1:8181/sensor/1/temp
                    String serviceHost = (url.split(":")[1].substring(2));
                    int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                    String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                    String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                    JSONObject temperature;

                    obj = new JSONObject(serviceReply);
                    String temp = obj.getJSONObject("sensor").getString("Pressure");
                    JOptionPane.showMessageDialog(self, "Pressure levels are at " + temp + " of 1023 ");
                    found = true;
                }
            }
            JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    if (!served) {
        try {
            ChatterBotFactory factory = new ChatterBotFactory();
            ChatterBot bot1 = factory.create(CLEVERBOT);
            ChatterBotSession bot1session = bot1.createSession();
            String s = input;
            String response = bot1session.think(s);
            JOptionPane.showMessageDialog(self, response, "Jarvis says", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception e) {
        }
    }

}

From source file:GUI.simplePanel.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    try {/*  w  w  w .  j a v a2  s .c o  m*/
        String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices");
        JSONObject obj;

        obj = new JSONObject(reply);
        JSONArray services = obj.getJSONArray("services");
        boolean found = false;
        for (int i = 0; i < services.length(); i++) {
            if (found) {
                return;
            }
            Object pref = services.getJSONObject(i).get("url");
            String url = (String) pref;
            if (url.contains("temp")) {
                // http://127.0.0.1:8181/sensor/1/temp
                String serviceHost = (url.split(":")[1].substring(2));
                int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]);
                String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/")));
                String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath);

                JSONObject temperature;

                obj = new JSONObject(serviceReply);
                String temp = obj.getJSONObject("sensor").getString("Temperature");
                JOptionPane.showMessageDialog(self,
                        "Temperature is " + temp.substring(0, temp.indexOf(".") + 2) + " Celsius");
                found = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}