从网站在android系统解析特定数据 [英] parsing particular data from website in android
问题描述
我在新的Android以及java.i想在我的Android应用程序的列表视图中显示来自网站的具体数据,然后保存这些数据的SQLite database.can谁能告诉我如何获取从所需的数据网站?
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?
其实我想从beginning.because我不理解怎么start.plz帮助我。我需要对以下问题的帮助就知道
1.how打开解析数据的一个项目?
2.什么将是建目标是什么?
3.how使用jsoup或任何其他进程的项目?
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?
我是全新的here.so,plz帮助我。
在此先感谢...
I am totally new here.so,plz help me. thanks in advance...
推荐答案
您可以使用JSoup解析。
You can use JSoup parse .
在这里你去 http://jsoup.org/cookbook/
JSoup是惊人的,非常有效的,你会在上面的链接找到很好的例子。
JSoup is amazing and very effective you will find good example in above link.
首先,你必须连接到要使用解析网页:
Document doc = Jsoup.connect("http://example.com/").get();
确保执行上述使用的AsyncTask或处理非UI线程code。
然后,您可以使用 JSoup选择语法。
例如,假设你要选择具有 ID
的 DIV
标签属性设置为全部内容测试
,你只需要使用:
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:
元素的div = doc.select(#DIV测试);
检索div的,那么你可以对他们使用迭代:
to retrieve the divs, then you can iterate on them using:
for (Element div : divs)
System.out.println(div.text());
}
下面是示例代码片断。
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.
如果你做异步操作再在非UI线程中执行它。
If you ever do Asynchronous operation then perform it in Non-UI Thread.
这篇关于从网站在android系统解析特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!