0

i am new in android as well as java.i want to display specific data from a website in my android app as a list view,and then save those data to sqlite database.can anyone tell me how to fetch desired data from website?

actually i want to know from the beginning.because i am not understanding how to start.plz help me.i need help about the following questions 1.how to open a project for parsing data? 2.what will be the built target? 3.how to use jsoup or any other process in the project?

I am totally new here.so,plz help me. thanks in advance...

sni
  • 33
  • 1
  • 4
  • Could you be more specific, do you want to copy text, links, images? And what have you tried? – Sam Jun 14 '12 at 05:06

2 Answers2

7

You can use JSoup parse .

Here you go http://jsoup.org/cookbook/

JSoup is amazing and very effective you will find good example in above link.

First, You have to connect to the webpage you want to parse using:

Document doc = Jsoup.connect("http://example.com/").get(); 

Make sure you execute above code in non-ui thread using Asynctask or handlers.

Then, you can select page elements using the JSoup selector syntax.

For instance, say you want to select all the content of the div tags with the id attribute set to test, you just have to use:

Elements divs = doc.select("div#test");

to retrieve the divs, then you can iterate on them using:

for (Element div : divs)
    System.out.println(div.text());
}

Below is Example Snippet.

package org.jsoup.examples;

import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Example program to list links from a URL.
 */
public class ListLinks {
    public static void main(String[] args) throws IOException {
        Validate.isTrue(args.length == 1, "usage: supply url to fetch");
        String url = args[0];
        print("Fetching %s...", url);

        Document doc = Jsoup.connect(url).get();
        Elements links = doc.select("a[href]");
        Elements media = doc.select("[src]");
        Elements imports = doc.select("link[href]");

        print("\nMedia: (%d)", media.size());
        for (Element src : media) {
            if (src.tagName().equals("img"))
                print(" * %s: <%s> %sx%s (%s)",
                        src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
                        trim(src.attr("alt"), 20));
            else
                print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
        }

        print("\nImports: (%d)", imports.size());
        for (Element link : imports) {
            print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
        }

        print("\nLinks: (%d)", links.size());
        for (Element link : links) {
            print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
        }
    }

    private static void print(String msg, Object... args) {
        System.out.println(String.format(msg, args));
    }

    private static String trim(String s, int width) {
        if (s.length() > width)
            return s.substring(0, width-1) + ".";
        else
            return s;
    }
}

Last but not list.

If you ever do Asynchronous operation then perform it in Non-UI Thread.

Vipul
  • 31,716
  • 7
  • 69
  • 86
0

There are many ways of retrieving data from web. The data could be in form of json string or xml both have different ways of parsing

for xml parsing we can use SAX parser OR DOM parser

to learn more about SAX parser check out this link: try this!!!

Anu
  • 554
  • 3
  • 9