Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Currently i am doing a project for my Master's thesis and have to evaluate web service standards for Web of Things applications(Real-time Patient Monitoring System)

Below are the codes I have written in soap and rest, in which communication takes place between Android App and Web Servers.The code is for sending and receiving Patient details.


1. Sending Soap request to server and getting XML response (SOAP Android APP)

Java
Document get_patient(){

    String NAMESPACE = PRESET_NAMESPACE;
    String URL = PRESET_URL;
    String SOAP_ACTION = PRESET_SOAP_ACTION+"/api/get_Patient_List";
    String METHOD_NAME = "get_Patient_List";


    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("hospital_name", Hospital_Name);

    SoapSerializationEnvelope envelope =
            new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        String resp= String .valueOf(envelope.getResponse());
        Document doc=convertStringToDocument(resp);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

private static Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try
    {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}


ArrayList<String> PatientName;
ArrayList<String> PatientCond;
ArrayList<String> AmbulanceId;
ArrayList<String> PatientId;

boolean showNotification=false;

void Check()
{
    final Document doc = get_patient();
    PatientName=new ArrayList<>();
    PatientId=new ArrayList<>();
    PatientCond=new ArrayList<>();
    AmbulanceId=new ArrayList<>();
    dhp.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            String result="null";
            try{
                result =doc.getElementsByTagName("status").item(0).getChildNodes().item(0).getNodeValue();
            }catch(Exception e){
                e.printStackTrace();
            }
            if (result.equals("true")) {

                int count =Integer.parseInt(doc.getElementsByTagName("count").item(0).getChildNodes().item(0).getNodeValue());
                showNotification=false;
                if(count>0)
                {

                    for(int i=0;i<count;i++)
                    {
                        AmbulanceId.add(String.valueOf(doc.getElementsByTagName("Patient_" + i).item(0).getChildNodes().item(1).getChildNodes().item(0).getNodeValue()));
                        PatientName.add(String.valueOf(doc.getElementsByTagName("Patient_" + i).item(0).getChildNodes().item(2).getChildNodes().item(0).getNodeValue()));
                        PatientCond.add(String.valueOf(doc.getElementsByTagName("Patient_" + i).item(0).getChildNodes().item(6).getChildNodes().item(0).getNodeValue()));
                        String ppid=String.valueOf(doc.getElementsByTagName("Patient_" + i).item(0).getChildNodes().item(3).getChildNodes().item(0).getNodeValue());
                        PatientId.add(ppid);

                        if(!al.contains(ppid)){
                            al.add(ppid);
                            showNotification=true;
                        }
                    }
                    ListView lv=(ListView) dhp.findViewById(R.id.DoctorlistView);

                    lv.setAdapter(new Doctor_Patient_List_CustomAdapter(dhp, PatientName, PatientCond, AmbulanceId, PatientId, type_of_user, hospital_name, uname, convertDocumentToString(doc)));
                }
            }else if(result.equals("false"))
            {
                ListView lv=(ListView) dhp.findViewById(R.id.DoctorlistView);
                lv.setAdapter(null);
            }


        }
    });


}


2. Getting SOAP request and sending XML response (SOAP Server)


C#
@WebMethod
public String get_Patient_List(@WebParam(name = "hospital_name") String hospital_name) {

    MongoCollection<org.bson.Document> collection = db.getCollection("patient_details");
    try {
        DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder icBuilder;
        icBuilder = icFactory.newDocumentBuilder();
        doc1 = icBuilder.newDocument();

        final Element mainRootElement = doc1.createElement("Response");
        doc1.appendChild(mainRootElement);

        FindIterable<org.bson.Document> iterable = collection.find(new org.bson.Document("hospital_name", hospital_name).append("is_enabled", "Yes"));

        datafound = false;
        count=0;
        iterable.forEach(new Block<org.bson.Document>() {
            @Override
            public void apply(final org.bson.Document document) {

                datafound = true;

                Element PatientElement = doc1.createElement("Patient_"+count);
                mainRootElement.appendChild(PatientElement);

                Element node = doc1.createElement("hospital_name");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("hospital_name"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("ambulance_id");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("ambulance_id"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("p_name");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("p_name"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("p_id");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("p_id"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("gender");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("gender"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("blood_grp");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("blood_grp"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("condition");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("condition"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("problem");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("problem"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("police_case");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("police_case"))));
                PatientElement.appendChild(node);

                node = doc1.createElement("is_enabled");
                node.appendChild(doc1.createTextNode(String.valueOf(document.get("is_enabled"))));
                PatientElement.appendChild(node);
                count++;

            }
        });


        if (datafound == true) {
            Element node = doc1.createElement("status");
            node.appendChild(doc1.createTextNode("true"));
            mainRootElement.appendChild(node);
            Element node1 = doc1.createElement("count");
            node1.appendChild(doc1.createTextNode(String.valueOf(count)));
            mainRootElement.appendChild(node1);
            return convertDocumentToString(doc1);
        }

        Element node = doc1.createElement("status");
        node.appendChild(doc1.createTextNode("false"));
        mainRootElement.appendChild(node);
        return convertDocumentToString(doc1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "null";
}



3. Sending Rest request to server and getting JSON response (Rest Android APP)

Java
JSONObject response1=null;
JSONObject get_patient(){

    RequestParams rp = new RequestParams();

    rp.add("hospital_name", Hospital_Name);

    response1=null;

    HttpUtils.get("/Patient/get_Patient_List", rp, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            response1=response;

        }

        @Override
        public void onFailure(int statusCode,
                              cz.msebera.android.httpclient.Header[] headers,
                              java.lang.Throwable throwable,
                              org.json.JSONObject errorResponse) {
            System.out.println("failed");
        }
    });

    return response1;
}

ArrayList<String> PatientName;
ArrayList<String> PatientCond;
ArrayList<String> AmbulanceId;
ArrayList<String> PatientId;

boolean showNotification=false;

void Check()
{
    final JSONObject doc = get_patient();
    PatientName=new ArrayList<>();
    PatientId=new ArrayList<>();
    PatientCond=new ArrayList<>();
    AmbulanceId=new ArrayList<>();
    dhp.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                String result = "null";
                try {
                    result = String.valueOf(doc.get("status"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (result.equals("true")) {
                    int count = Integer.parseInt(String.valueOf(doc.get("count")));
                    showNotification = false;
                    if (count > 0) {
                        for (int i = 0; i < count; i++) {
                            JSONObject jo=new JSONObject(String.valueOf(doc.getJSONArray("Patient_" + i).get(0)));
                            AmbulanceId.add(String.valueOf(jo.get("ambulance_id")));
                            PatientName.add(String.valueOf(jo.get("p_name")));
                            PatientCond.add(String.valueOf(jo.get("condition")));
                            String ppid = String.valueOf(jo.get("p_id"));
                            PatientId.add(ppid);

                            if (!al.contains(ppid)) {
                                al.add(ppid);
                                showNotification = true;
                            }
                        }
                        ListView lv = (ListView) dhp.findViewById(R.id.DoctorlistView);

                        lv.setAdapter(new Doctor_Patient_List_CustomAdapter(dhp, PatientName, PatientCond, AmbulanceId, PatientId, type_of_user, hospital_name, uname, String.valueOf(doc)));
                    }
                } else if (result.equals("false")) {
                    ListView lv = (ListView) dhp.findViewById(R.id.DoctorlistView);
                    lv.setAdapter(null);
                }
            } catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}}



4. Getting Rest request and sending JSON response (REST Server)



Java
@GET
@Path("/get_Patient_List")
@Produces("application/json")
public String get_Patient_List(@QueryParam("hospital_name") String hospital_name) {
    obj = new JSONObject();
    try {
        FindIterable<org.bson.Document> iterable = collection.find(new org.bson.Document("hospital_name", hospital_name).append("is_enabled", "Yes"));
        datafound = false;
        count=0;
        iterable.forEach(new Block<org.bson.Document>() {
            @Override
            public void apply(final org.bson.Document document) {

                ja = new JSONArray();
                JSONObject jo = new JSONObject();

                datafound = true;
                jo.put("hospital_name", String.valueOf(document.get("hospital_name")));
                jo.put("ambulance_id", String.valueOf(document.get("ambulance_id")));
                jo.put("p_name", String.valueOf(document.get("p_name")));
                jo.put("p_id", String.valueOf(document.get("p_id")));
                jo.put("gender", String.valueOf(document.get("gender")));
                jo.put("blood_grp", String.valueOf(document.get("blood_grp")));
                jo.put("condition", String.valueOf(document.get("condition")));
                jo.put("problem", String.valueOf(document.get("problem")));
                jo.put("police_case", String.valueOf(document.get("police_case")));
                jo.put("is_enabled", String.valueOf(document.get("is_enabled")));
                ja.put(jo);
                obj.put("Patient_"+count, ja);
                count++;
            }
        });

        if (datafound == true) {
            obj.put("status", "true");
            obj.put("count", count);
            return String.valueOf(obj);
        }

        obj.put("status", "false");
        return String.valueOf(obj);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "null";
}}



I need a evaluation for these code on the basis of these points:-

1. Which code (SOAP or REST) is more expressive, meaning which provides better solution in clear, natural and concise way?


2. Which code is easier to use and understand ?



What I have tried:

I evaluated this code from my side but i also need to know others view about the code as i need to include a survey in my thesis.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900