I have a post method. That consumes a Webservice that returns a boolean (ws) (everything correct up to here), as you know it returns a true or false (which makes it correct)
How can I retrieve 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 me
<?xml version="1.0" encoding="utf-8"?><boolean xmlns="http://pda.gim/">true</boolean>
What is commonly done is to create a
BufferedReader
to store the response value,From this response, which is an xml, a parse is made to obtain the value of the "boolean" tag, when determining its value you can use the logic you want in your program:
this is a method used in the previous code, in which the response and the name of the tag (in this case "boolean") are sent and we obtain its value:
This method can be useful to determine the value of any tag in your xml response.
In order 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 you a method that can help you convert aString
to aDocument
Now that you have your object
Document
, you can access the propertyboolean
you are looking for with the following code:booleanValue
will now have the value of true as per your example.The Apache HttpCore API provides a Utility class to get the String value of an object
HttpEntity
:Once the String value is obtained, it would only be necessary to perform the Cast to the required value (in this case to boolean)
To achieve what you want, first you have to obtain the content of the response, you achieve this by passing the content of the response
EntityUtils.toString()
as a parameter and it converts it toString
.To read the obtained xml you can use various libraries, such as the JDOM library . To use the JDOM library you must integrate it into your project. You can download the library jar from here and integrate it manually, 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 build tools.
With the integrated library you already have everything you need to read the xml.