5

i would like to parse out some text from a page.

Is there an easy way to save the product info in to a string for example? Example url: http://upcdata.info/upc/7310870008741

Thanks

3 Answers 3

9

Jsoup is excellent at parsing simple HTML from Android applications:

http://jsoup.org/

To get the page, just do this:

URL url = new URL("http://upcdata.info/upc/7310870008741");
Document document = Jsoup.parse(url, 5000);

Then you can parse out whatever you need from the Document. Check out this link for a brief description of how to extract parts of the page:

http://jsoup.org/cookbook/extracting-data/dom-navigation

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to read from a URL into a String:

StringBuffer myString = new StringBuffer();
try {
    String thisLine;
    URL u = new URL("http://www.google.com");
    DataInputStream theHTML = new DataInputStream(u.openStream());
    while ((thisLine = theHTML.readLine()) != null) {
        myString.append(thisLine);
    } 
} catch (MalformedURLException e) {

} catch (IOException e) {

}

// call toString() on myString to get the contents of the file your URL is
// pointing to.

This will give you a plain old string, HTML markup and all.

Comments

1
String tmpHtml = "<html>a whole bunch of html stuff</html>"; 
String htmlTextStr = Html.fromHtml(tmpHtml).toString(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.