how do I get the correct value to an HttpResponse response = mHttpClient.execute (httppost);

2

I have a post method. That consumes a Webservice that returns a boolean (ws) (everything is correct up here), as you know, it returns a true or false (which makes it correct)

How can I recover this result?

This is the code

HttpResponse response = mHttpClient.execute(httppost); 
            final HttpEntity entity = response.getEntity(); 
            if (entity == null) {
                Log.w("GIM", "The response has no entity.");
            } else {
                // que pongo acá para obtener el valor true o false
            }

the entity value returns

<?xml version="1.0" encoding="utf-8"?><boolean xmlns="http://pda.gim/">true</boolean>
    
asked by Gerard_jcr 02.10.2017 в 19:41
source

4 answers

2

What is commonly done is to create a BufferedReader to store the value of the answer,

<?xml version="1.0" encoding="utf-8"?><boolean xmlns="http://pda.gim/">true</boolean>

From this answer, which is an xml, a parsing is performed to obtain the value of the "boolean" tag, when determining its value you can use the logic you want in your program:

StringBuilder sb = new StringBuilder();
try {
    BufferedReader reader = 
           new BufferedReader(new InputStreamReader(entity.getContent()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }


    //En base a respuesta realiza parsing para obtener el valor del tag boolean.
    if (parseXml(sb.toString(), "boolean").equals("true")){
       //Contiene valor true.        
    }else{
       //No contiene valor true, por lo tanto es false.        
    } 

}catch (IOException e) {
 e.printStackTrace(); 
}catch (Exception e) { 
e.printStackTrace(); 
}

This is a method used in the previous code, in which the response and the name of the tag are sent (in this case "boolean") and we obtain its value:

  public String parseXml(String xml, String tag){
        try {
            boolean obtieneValor = false;
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput( new StringReader(xml)); 
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if(eventType == XmlPullParser.START_DOCUMENT) {
                    //Log.d(TAG,"Start document");
                } else if(eventType == XmlPullParser.START_TAG) {
                    //Log.d(TAG,"Start tag "+xpp.getName());
                    if(xpp.getName().equals(tag)) {
                        obtieneValor = true;
                    }
                } else if(eventType == XmlPullParser.END_TAG) {
                    //Log.d(TAG,"End tag "+xpp.getName());
                } else if(eventType == XmlPullParser.TEXT) {
                    //Log.d(TAG,"valor Text "+xpp.getText());
                    if(obtieneValor && xpp.getText()!= null){
                       return xpp.getText();
                    }
                    obtieneValor = false;
                }
                eventType = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

This method can be useful to determine the value of any tag in your xml response.

String valor =  parseXml(<respuesta XML>, <nombre de tag>);
    
answered by 02.10.2017 / 21:30
source
0

To be able to access the value of the response, you can create a Document with which your XML response becomes an object that you can manipulate and access its properties, I leave a method that can help you convert a String to% Document

public static Document documentoDesdeString(String xml) throws Exception {
    DocumentBuilderFactory factory = 
    DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

Since you have your Document object, you can access the boolean property that you are looking for with the following code:

try {
    Document doc = documentoDeString(xml);
    Node xmlBoolean = doc.getElementsByTagName("boolean").item(0);
    String booleanValue = xmlBoolean.getFirstChild().getNodeValue();
} catch (Exception e) {
    e.printStackTrace();
}

booleanValue will now have the value of true according to your example.

    
answered by 02.10.2017 в 20:18
0

The API of Apache HttpCore provides an appliance class to get the String value of an object HttpEntity :

String valorString = EntityUtils.toString(entity);

Once the String value has been obtained, you only have to cast the required value (in this case a Boolean)

if(valorString != null){
    Boolean valor = (Boolean)valorString;
}
    
answered by 02.10.2017 в 20:22
0

To achieve what you want you must first obtain the content of the answer, that you achieve with EntityUtils.toString() to which you pass as parameter the content of the answer and this converts it to String .

To read the obtained xml you can use several libraries, such as the JDOM library. To use the JDOM library, you must integrate it into your project. You can download the jar from the library from here and integrate it manually, in this tutorial explains how to integrate libraries manually in Eclipce. Or you can integrate it using the dependencies provided by the library, here you can find dependencies to integrate it with Gradle, Maven or other construction tools.

With the integrated library you already have everything you need to read the xml.

    HttpResponse response = mHttpClient.execute(httppost); 
    final HttpEntity entity = response.getEntity(); 
    if (entity == null) {
        Log.w("GIM", "The response has no entity.");
    } else {

        SAXBuilder saxBuilder = new SAXBuilder();
        Document doc = null;

        try {

            // Obtienes el xml de la respuesta
            String xml = EntityUtils.toString(entity);

            // Pasas el xml a saxBuilder para poder leerlo y extraer la informacion
            doc = saxBuilder.build(new StringReader(xml));
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Obtienes el valor almacenado en el xml
        String valor = doc.getRootElement().getText();

        // Imprimes el valor
        System.out.println(message);
    
answered by 02.10.2017 в 21:12