我正在研究一個需要從鏈接中獲取網頁源代碼的應用程序,然後解析該網頁中的html。如何從android中的html鏈接獲取頁面的html源代碼?
你能給我一些例子,或者從哪裏開始寫點什麼開始寫這樣的應用程序?
我正在研究一個需要從鏈接中獲取網頁源代碼的應用程序,然後解析該網頁中的html。如何從android中的html鏈接獲取頁面的html源代碼?
你能給我一些例子,或者從哪裏開始寫點什麼開始寫這樣的應用程序?
您可以使用HttpClient像這樣執行HTTP GET和檢索HTML響應,東西:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
public class RetrieveSiteData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
StringBuilder builder = new StringBuilder(100000);
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
builder.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return builder.toString();
}
@Override
protected void onPostExecute(String result) {
}
}
它正在工作,但很慢.. – Nepster 2014-05-22 11:08:14
這樣稱呼它
new RetrieveFeedTask(new OnTaskFinished()
{
@Override
public void onFeedRetrieved(String feeds)
{
//do whatever you want to do with the feeds
}
}).execute("http://enterurlhere.com");
RetrieveFeedTask.class
class RetrieveFeedTask extends AsyncTask<String, Void, String>
{
String HTML_response= "";
OnTaskFinished onOurTaskFinished;
public RetrieveFeedTask(OnTaskFinished onTaskFinished)
{
onOurTaskFinished = onTaskFinished;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls)
{
try
{
URL url = new URL(urls[0]); // enter your url here which to download
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null)
{
// System.out.println(inputLine);
HTML_response += inputLine;
}
br.close();
System.out.println("Done");
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return HTML_response;
}
@Override
protected void onPostExecute(String feed)
{
onOurTaskFinished.onFeedRetrieved(feed);
}
}
OnTaskFinished.java
public interface OnTaskFinished
{
public void onFeedRetrieved(String feeds);
}
我在哪裏定義'url_search'和'HTML_Resonse'? – user1091524 2014-12-25 20:45:41
另外,Parser(feed)是什麼;參考? Alt_Enter找不到Parser類。 – user1091524 2014-12-25 22:01:18
請修正這個錯誤'Cannot resolve symbol'HTML_Resonse''and'Cannot resolve method'Parser(java.lang.String)''@Nepster – Florida 2015-06-16 05:55:23
這個問題有點舊,但我想我現在應該發佈我的答案,現在DefaultHttpClient
,HttpGet
等已被棄用。給定一個URL,這個函數應該得到並返回HTML。
public static String getHtml(String url) throws IOException {
// Build and set timeout values for the request.
URLConnection connection = (new URL(url)).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
// Read and store the result line by line then return the entire string.
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder html = new StringBuilder();
for (String line; (line = reader.readLine()) != null;) {
html.append(line);
}
in.close();
return html.toString();
}
你能告訴我如何將這個函數數據發送到mainActivity的webview onload嗎? – user1788736 2017-11-20 00:21:15
這是不完全清楚你想要做什麼?我想你想要得到的網頁,然後解析HTML? – Janusz 2010-03-11 08:43:37
我正在處理HTML解析。第一個任務,我想從我的HTML鏈接獲取HTML源代碼。怎麼做?對不起我最差的英語。感謝鼓勵我。 – Praveen 2010-03-11 09:09:46
沒問題我試着重新解釋一下你的問題。我希望仍然是同樣的問題:)對於進一步的問題,你的問題是非常廣泛的。我們喜歡有點特別的問題,並且在您的應用中遇到單個問題,可能會使用一些示例代碼來解釋您的問題...... – Janusz 2010-03-11 09:24:15