導航:首頁 > 操作系統 > androidjsoup使用

androidjsoup使用

發布時間:2022-10-03 13:13:52

android app jsoup網頁中帶有操作的怎麼讀取

需要對WebView設置如下:

private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);

// if (savedInstanceState == null) {
// getSupportFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment()).commit();
// }
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(

❷ Android用jsoup解析html時如果<td>標簽有第四行我該如何提取第四行

很簡單,可以這樣寫


Documentdoc=Jsoup.parse(input,"UTF-8","你的這個網址");
Elementtd=doc.select(".table002>tr:eq(0)>td:eq(3)");
//得到第四個td
Elementtd=doc.select(".table002>tr:eq(0)>td:eq(4)");
//得到第五個td

❸ 大俠們我想問兩個Android開發使用jsoup解析html的相關問題

問題:大俠們我想問兩個Android開發使用jsoup解析html的相關問題

回答:第一個問題幫你解決了;第二個問題很簡單(但你用錯了),但沒有你的這個htm文件,沒有修改測試

java">super.onCreate(savedInstanceState);
TextViewtxt=newTextView(this);
InputStreaminput;
try{
input=getResources().getAssets().open("temp04_assets/txt01.htm");
intbuffersize=input.available();//取得輸入流的位元組長度
bytebuffer[]=newbyte[buffersize];
input.read(buffer);//將數據讀入數組
input.close();//讀取完畢後要關閉流。
Stringtxthtml=EncodingUtils.getString(buffer,"UTF-8");//設置取得的數據編碼,防止亂碼
Documentdoc=Jsoup.parse(txthtml);
ElementinfoTable=doc.getElementsByAttributeValue("class",
"table002").first();
ElementstableLineInfos=infoTable.select("tr");
for(ElementlineInfo:tableLineInfos){
StringlineInfoContent=lineInfo.select("td").last().text()
.trim();
txt.setText(lineInfoContent);
setContentView(txt);
}
}catch(IOExceptionerr){
err.getStackTrace();
}

❹ android通過jsoup解析assets中的html文件該如何執行

Android java代碼如下:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
Document doc = Jsoup.connect("http://www.example.com").timeout(60000).get();
Elements ps = doc.select("p.my p");
StringBuilder linkBuffer = new StringBuilder();
if (ps != null) {
for (Element p : ps) {
Elements links = p.select("a[href]");
if (null != links) {
for (Element link : links) {
linkBuffer.append(link.attr("abs:href"));//相對地址會自動轉成絕對url地址
linkBuffer.append(" ");
linkBuffer.append(link.text());
}
}
}
}
對於Jsoup更詳細的信息,可以看官網的文檔。

參考連接:http://www.th7.cn/Program/java/2011/12/07/49658.shtml

❺ android中用jsoup解析HTML得到NULL,代碼在jsoup的官方網頁上是可以的。代碼如下:

你好:
話說有報錯提示么?應該聲明Internet許可權了吧?沒提示的話 就只能斷點跟一遍了``

❻ android 中使用 jsoup

不要放到主線程裡面啊。
希望對你能有所幫助。

❼ android 怎麼用jsoup爬別人網站的數據當做介面使用

下面這個鏈接有介紹jsoup的詳細例子。其實過程就是使用urlconnect或者httpclient獲取html文本文件,然後使用jsoup解析html。
http://blog.csdn.net/androidwuyou/article/details/52636821

❽ jsoup 能否載入非同步的數據 載入的url 中含有非同步的 展現的數據,怎麼用jsoup 獲得

package com.javen.Jsoup;

import java.io.IOException;

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

public class JsoupTest {
static String url="http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub
BolgBody();
//test();
//Blog();
/*
* Document doc = Jsoup.connect("http://www.oschina.net/")
* .data("query", "Java") // 請求參數 .userAgent("I 』 m jsoup") // 設置
* User-Agent .cookie("auth", "token") // 設置 cookie .timeout(3000) //
* 設置連接超時時間 .post();
*/// 使用 POST 方法訪問 URL

/*
* // 從文件中載入 HTML 文檔 File input = new File("D:/test.html"); Document doc
* = Jsoup.parse(input,"UTF-8","http://www.oschina.net/");
*/
}

/**
* 獲取指定HTML 文檔指定的body
* @throws IOException
*/
private static void BolgBody() throws IOException {
// 直接從字元串中輸入 HTML 文檔
String html = "<html><head><title> 開源中國社區 </title></head>"
+ "<body><p> 這里是 jsoup 項目的相關文章 </p></body></html>";
Document doc = Jsoup.parse(html);
System.out.println(doc.body());

// 從 URL 直接載入 HTML 文檔
Document doc2 = Jsoup.connect(url).get();
String title = doc2.body().toString();
System.out.println(title);
}

/**
* 獲取博客上的文章標題和鏈接
*/
public static void article() {
Document doc;
try {
doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/").get();
Elements ListDiv = doc.getElementsByAttributeValue("class","postTitle");
for (Element element :ListDiv) {
Elements links = element.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text().trim();
System.out.println(linkHref);
System.out.println(linkText);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
/**
* 獲取指定博客文章的內容
*/
public static void Blog() {
Document doc;
try {
doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get();
Elements ListDiv = doc.getElementsByAttributeValue("class","postBody");
for (Element element :ListDiv) {
System.out.println(element.html());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

下面來介紹android中使用Jsoup非同步解析網頁的數據 請注意: 這里很容易遇到一個亂碼的問題

配置文件:AndroidManifest.xml中加 許可權 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
layout的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="200dp" />

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</ScrollView>

</LinearLayout>

主要非同步載入數據的代碼

package com.javen.aaa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

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

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;

public class MainActivity extends Activity {
private WebView webView;
private TextView textView;
private static final int DIALOG_KEY = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
textView=(TextView) findViewById(R.id.textView);
try {
ProgressAsyncTask asyncTask=new ProgressAsyncTask(webView,textView);
asyncTask.execute(10000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public String test() {
StringBuffer buffer=new StringBuffer();
Document doc;
try {
doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/").get();
Elements ListDiv = doc.getElementsByAttributeValue("class","postTitle");
for (Element element :ListDiv) {
Elements links = element.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text().trim();
buffer.append("linkHref=="+linkHref);
buffer.append("linkText=="+linkText);

System.out.println(linkHref);
System.out.println(linkText);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer.toString();

}

// 彈出"查看"對話框
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_KEY: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("獲取數據中 請稍候...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}

public static String readHtml(String myurl) {
StringBuffer sb = new StringBuffer("");
URL url;
try {
url = new URL(myurl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "gbk"));
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}

class ProgressAsyncTask extends AsyncTask<Integer, Integer, String> {

private WebView webView;
private TextView textView;
public ProgressAsyncTask(WebView webView,TextView textView) {
super();
this.webView=webView;
this.textView=textView;
}

/**
* 這里的Integer參數對應AsyncTask中的第一個參數 這里的String返回值對應AsyncTask的第三個參數
* 該方法並不運行在UI線程當中,主要用於非同步操作,所有在該方法中不能對UI當中的空間進行設置和修改
* 但是可以調用publish Progress方法觸發onProgressUpdate對UI進行操作
*/
@Override
protected String doInBackground(Integer... params) {
String str =null;
Document doc = null;
try {
// String url ="http://www.cnblogs.com/zyw-205520/p/3355681.html";
//
// doc= Jsoup.parse(new URL(url).openStream(),"utf-8", url);
// //doc = Jsoup.parse(readHtml(url));
// //doc=Jsoup.connect(url).get();
// str=doc.body().toString();
doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get();
Elements ListDiv = doc.getElementsByAttributeValue("class","postBody");
for (Element element :ListDiv) {
str=element.html();
System.out.println(element.html());
}
Log.d("doInBackground", str.toString());
System.out.println(str);
//你可以試試GBK或UTF-8
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str.toString() ;
//return test();
}

/**
* 這里的String參數對應AsyncTask中的第三個參數(也就是接收doInBackground的返回值)
* 在doInBackground方法執行結束之後在運行,並且運行在UI線程當中 可以對UI空間進行設置
*/
@Override
protected void onPostExecute(String result) {
webView.loadData(result, "text/html;charset=utf-8", null);
textView.setText(result);
removeDialog(DIALOG_KEY);
}

// 該方法運行在UI線程當中,並且運行在UI線程當中 可以對UI空間進行設置
@Override
protected void onPreExecute() {
showDialog(DIALOG_KEY);
}

/**
* 這里的Intege參數對應AsyncTask中的第二個參數
* 在doInBackground方法當中,,每次調用publishProgress方法都會觸發onProgressUpdate執行
* onProgressUpdate是在UI線程中執行,所有可以對UI空間進行操作
*/
@Override
protected void onProgressUpdate(Integer... values) {

}
}

}

閱讀全文

與androidjsoup使用相關的資料

熱點內容
phpmysql自增 瀏覽:164
把ppt保存為pdf 瀏覽:533
汽車密封件加密配件 瀏覽:887
黑馬程序員15天基礎班 瀏覽:560
java調整格式 瀏覽:521
香港雲伺服器租用價 瀏覽:78
linuxsublime3 瀏覽:560
imac混合硬碟命令 瀏覽:277
沈陽用什麼app租房車 瀏覽:857
00後高中生都用什麼app 瀏覽:237
戴爾塔式伺服器怎麼打開獨立顯卡 瀏覽:807
醫療程序員招聘 瀏覽:597
住宿app可砍價是什麼意思 瀏覽:133
java跳出語句 瀏覽:55
javastring個數 瀏覽:928
人工免疫演算法應用 瀏覽:79
有什麼app能收聽俄羅斯廣播電台 瀏覽:34
2015考研紅寶書pdf 瀏覽:443
程序員幾月跳槽合適 瀏覽:444
液壓油可壓縮嗎 瀏覽:946