Search code examples
androidhttp-posthttpresponseandroidhttpclient

How to parse HTML response in android


I am sending data to server using HttpPost in android which is successfully received by server. Now the server response is as follows:

<html><body>
<input type="text" id="nameL" name="nameL" value="Sam" />
<input type="text" id="level" name="level" value="Introductory"  />
</body></html>

How can I get value of http response using the input name attribute (e.g. nameL & level) on an Android client? I am able to get the string response but it contains all tags <html>....</html> (as above) in one string.


Solution

  • Use an HTML parser. I personally like jsoup:

    Document doc = JSoup.parse(responseString);
    String nameL = doc.select("input[name=nameL]").attr("value");
    String level = doc.select("input[name=level]").attr("value");
    

    However, as VenomVendor pointed out, it is unusual (and inefficient, by the way) to use HTML for API-like communication.